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 command question

Author  Topic 

skiabox
Posting Yak Master

169 Posts

Posted - 2008-11-26 : 19:06:55
I want to insert a value in a specific column of a table only if it does not exist in this column.
Is this possible?
Thank you very much.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-26 : 19:40:31
This will give you startup.

If exists (select * from table where column = value)
begin
raiserror('Records already exists',10,1)
end
else
begin
insert table (col......)
values(........)
end
Go to Top of Page

skiabox
Posting Yak Master

169 Posts

Posted - 2008-11-26 : 19:45:52
thnx a lot for the answer.
Is this possible for the server to do nothing instead of the raiserror command you entered?
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-26 : 19:50:59
Yes you can.

If not exists (select * from table where column = value)
begin
insert table values....
end
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-11-27 : 01:07:32
try this
INSERT INTO table
SELECT value
FROM NOT EXISTS ( SELECT 1 FROM table WHERE column = value)


"There is only one difference between a dream and an aim.
A dream requires soundless sleep to see,
whereas an aim requires sleepless efforts to achieve..!!"
Go to Top of Page
   

- Advertisement -