SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 insert into where record does not exist
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tech
Starting Member

32 Posts

Posted - 05/30/2012 :  19:44:47  Show Profile  Reply with Quote
I have 2 tables:

users
userattributes

I wish to insert data into userattributes where the userID in does not exist from the users table into the userattributes table.

how would I do this?

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/30/2012 :  21:07:23  Show Profile  Reply with Quote

INSERT INTO userattributes ( userID, . . . )
SELECT u.userID, . . .
FROM   users u
WHERE  NOT EXISTS (SELECT * FROM userattributes a WHERE a.userID = u.userID)



KH
Time is always against us

Go to Top of Page

SQL Basic
Starting Member

Canada
8 Posts

Posted - 05/31/2012 :  06:02:39  Show Profile  Reply with Quote
Also, you can modify with this code below :

INSERT INTO table2 (file_index, celeb_name)
SELECT file_index, 'userID'
FROM table1
WHERE filename = 'user'
AND file_index NOT IN (SELECT DISTINCT file_index
FROM table2
WHERE user_name = 'userID')
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000