Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert error

Author  Topic 

mana
Posting Yak Master

102 Posts

Posted - 2014-09-24 : 04:44:13
how should i use not exists
can you help me please?
for example the below code doesn't work
i have 5 primary keys : datum, teil,id,workcenter and reason-code


hello
i have the below code and it has errro for primary key
can you help me pleasE?
in both
dbo.Table_2 and dbo.Bewegungen_Inventur_Korrekturen

Datum_Bewegung
and Teil_Nummer
and Workcenter
and Reason_Code
and Id_Nr
are primary key together
thank you


insert into dbo.Table_2
SELECT b.Datum_Bewegung, b.Teil_Nummer, b.Workcenter, b.Reason_Code, b.Menge,
b.Id_Nr, b.Mengeneinheit FROM dbo.Bewegungen_Inventur_Korrekturen b
WHERE not exists (SELECT * FROM dbo.Table_2 a WHERE b.Datum_Bewegung = a.Datum_Bewegung
and b.Teil_Nummer=a.Teil_Nummer
and b.Workcenter=a.Workcenter
and b.Reason_Code=a.Reason_Code
and b.Id_Nr=a.Id_Nr)

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2014-09-24 : 11:46:20
The code *seems* right. Could it be that your source table (Bewegungen_Inventur_Korrekturen) has duplicates? I'd also use an explicit column list for the INSERT; it is a best practice.



Too often we enjoy the comfort of opinion without the discomfort of thought. - John F. Kennedy
Go to Top of Page
   

- Advertisement -