Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server IF EXISTS

Tags:

sql-server

I have to insert data into table but only if it does not already exist.

I check for existing rows using:

IF EXISTS (SELECT 'X' FROM Table1 where id = @id)

Will the use of 'X' improve performance instead of using a column name?

like image 767
karthik Avatar asked Feb 27 '23 00:02

karthik


1 Answers

No. You can use *,column name, NULL or even 1/0.

As per the ANSI standard, it should not be evaluated. Page 191 ANSI SQL 1992 Standard.

* is mentioned in MSDN

However, a better way is to use MERGE (SQL Server 2008) or simply catch the error. Previous SO answers from me: One, Two

like image 187
gbn Avatar answered May 26 '23 06:05

gbn