How to add a record only if it does not already exist in SQL Server?

I have a C # project with a Sql server database.
In this database, I have a table named "Process" and columns named "process_name", "full_name" and "version" (all of type: nvarchar (50)).
I want to write a query that will add a new process only if it does not already exist in the table.
How can i do this?
Many thanks,

+4
source share
3 answers
IF NOT EXISTS (SELECT * FROM Process WHERE process_name = 'xxx') INSERT INTO Process (process_name, Full_Name, Version) VALUES ('xxx', 'yyy', 'zzz') 
+12
source

You might be interested in the MERGE command, which is new to SQL Server 2008.

http://technet.microsoft.com/en-us/library/bb510625.aspx

This allows you to insert rows that do not exist, or update records that all exist in a single expression.

+3
source

Assuming process_name is the PC you want to check:

 IF NOT EXISTS(SELECT 1 FROM Process WHERE process_name = @ProcessName) BEGIN -- Process does not already exist, so INSERT END 
+1
source

Source: https://habr.com/ru/post/1308249/


All Articles