Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I do have a table called "Orders" and its structure is like thisOrderId Name Address Cityand there is second table called "Data" with structureId ShipName ShipAddress ShipCityNow I want to insert records into "Data" table from "Orders" table which are not already present in "Data" table. If the record already exists in "Data" table it shouldnt be inserted.How to write a query ????????
cognos79
Posting Yak Master
241 Posts
Posted - 2006-11-07 : 16:19:51
ronin...this is not what i need. you get my question wrong.
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2006-11-07 : 16:21:54
This assumes that [ID] and OrderID is unique (in both tables).
insert [data] ([Id] ,ShipName ,ShipAddress ,ShipCity)select OrderId ,[Name] ,Address ,Cityfrom Orders owhere not exists ( select 'cognos79' from [data] d where d.[id] = o.OrderID )
Be One with the OptimizerTG
ronin2307
Posting Yak Master
126 Posts
Posted - 2006-11-07 : 16:24:51
I must have but TG already gave you an answer
cognos79
Posting Yak Master
241 Posts
Posted - 2006-11-07 : 16:25:39
TG...The Primary Key is composed of three fields not just OrderId field. How can i do it then