| Author |
Topic |
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2008-08-12 : 16:15:31
|
Hi,I have to update a table with mutple rows that I am getting from an In list. This is the query that I have to get the rows I need inserted.Select CustomerIDFrom dbo.CustomerWhere IsRegistered = 1And CustomerID Not In ( Select CustomerID From dbo.WheyCool_Member_Refer ) I need to add all of those rows to tableb. How do I do that without a loop or a cursor?Table b Looks like this. The list from above goes into cid.id cid mid1 123 9662 134 9663 187 9664 345 9665 654 9666 345 9667 765 966 Thanks,JBelthoff• Hosts Station is a Professional Asp Hosting Provider› As far as myself... I do this for fun! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-12 : 16:19:56
|
| First, you should be using a join:Select c.CustomerIDFrom dbo.Customer cjoin w.WheyCool_Member_Refer on c.CustomerID = w.CustomerIDWhere c.IsRegistered = 1And now use it in an update:UPDATE bSET cid = c.CustomerIDFROM tableb bINNER JOIN dbo.Customer c on b.cid = c.CustomerIDjoin w.WheyCool_Member_Refer on c.CustomerID = w.CustomerIDWhere c.IsRegistered = 1Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2008-08-12 : 16:23:40
|
| I don't need to update tablb I need to insert those additional rows.JBelthoff• Hosts Station is a Professional Asp Hosting Provider› As far as myself... I do this for fun! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-12 : 16:25:26
|
Ok, you said update and insert in your post, so I wasn't sure:quote: I have to update a table
Just use this then:INSERT INTO tableb (cid)Select c.CustomerID, 966From dbo.Customer cjoin w.WheyCool_Member_Refer on c.CustomerID = w.CustomerIDWhere c.IsRegistered = 1Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 16:26:34
|
I think you mean a LEFT JOIN with WHERE ... IS NULL,since OP wrote "NOT IN"... E 12°55'05.25"N 56°04'39.16" |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-12 : 16:28:28
|
| New version:INSERT INTO tableb (cid, mid)Select c.CustomerID, 966From dbo.Customer cleft join w.WheyCool_Member_Refer on c.CustomerID = w.CustomerIDWhere c.IsRegistered = 1 and w.CustomerID IS NULLTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2008-08-12 : 16:29:50
|
| OK That should do it.Thanks,JBelthoff• Hosts Station is a Professional Asp Hosting Provider› As far as myself... I do this for fun! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 16:30:22
|
[code]INSERT TableB ( cid, mid )SELECT DISTINCT c.CustomerID, 966FROM Customer AS cLEFT JOIN WheyCool_Member_Refer AS wmr ON wmr.CustomerID = c.CustomerIDWHERE c.IsRegistered = 1 AND wmr.CustomerID IS NULL AND NOT EXISTS (SELECT * FROM TableB AS x WHERE x.cid = c.CustomerID)[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|