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 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)endelsebegininsert table (col......)values(........)end
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?
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
PeterNeo
Constraint Violating Yak Guru
357 Posts
Posted - 2008-11-27 : 01:07:32
try this
INSERT INTO tableSELECT valueFROM 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..!!"