Damien Damien - 9 months ago 45
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')