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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert from an In List

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 CustomerID
From dbo.Customer
Where IsRegistered = 1
And 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 mid
1 123 966
2 134 966
3 187 966
4 345 966
5 654 966
6 345 966
7 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.CustomerID
From dbo.Customer c
join w.WheyCool_Member_Refer on c.CustomerID = w.CustomerID
Where c.IsRegistered = 1

And now use it in an update:

UPDATE b
SET cid = c.CustomerID
FROM tableb b
INNER JOIN dbo.Customer c on b.cid = c.CustomerID
join w.WheyCool_Member_Refer on c.CustomerID = w.CustomerID
Where c.IsRegistered = 1


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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!
Go to Top of Page

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, 966
From dbo.Customer c
join w.WheyCool_Member_Refer on c.CustomerID = w.CustomerID
Where c.IsRegistered = 1


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-12 : 16:27:52
Missed the NOT IN, yes indeed need a LEFT JOIN WHERE ... IS NULL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-12 : 16:28:28
New version:

INSERT INTO tableb (cid, mid)
Select c.CustomerID, 966
From dbo.Customer c
left join w.WheyCool_Member_Refer on c.CustomerID = w.CustomerID
Where c.IsRegistered = 1 and w.CustomerID IS NULL

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-12 : 16:30:22
[code]INSERT TableB
(
cid,
mid
)
SELECT DISTINCT c.CustomerID,
966
FROM Customer AS c
LEFT JOIN WheyCool_Member_Refer AS wmr ON wmr.CustomerID = c.CustomerID
WHERE 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"
Go to Top of Page
   

- Advertisement -