Damien Damien - 1 year ago 69
SQL Question

SQL Server : INSERT with a condition

I want to insert into my

table only if the values I'm trying to pass isn't in there already.

I tried:

INSERT INTO [Files-Favorites](fileID,auditorID)
VALUES ('1', '34')
WHERE (fileID != '1' AND auditorID != '34')

This doesn't work. I'm trying not to
duplicate values. How do I pull this off? This is for a Microsoft SQL Server 2005. Thank you

Answer Source

Try using if not exists

IF NOT EXISTS(SELECT * FROM [Files-Favorites] WHERE fileID = '1' AND auditorID = '34') 
    INSERT INTO [Files-Favorites](fileID, auditorID) 
    VALUES('1', '34') 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download