i have to insert data into table, before that have to check wherther same id is already exist or not. I checked like "IF EXISTS ( SELECT 'X' FROM Table1 where id =@id)". Will this improve performance instread of column name ?
A:
It's probably negligible difference. I think the most common "pattern" I've seen is to just select 1 when you're just checking for existence, but I wouldn't worry about it too much.
select 1 from Table1...
Andy White
2010-10-22 05:09:01
+2
A:
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
gbn
2010-10-22 05:09:43
@gbn what about unique key for that column?
anishmarokey
2010-10-22 05:55:00
@anishmarokey: see my links to my other answers. A unique key in itself does not help, but it does allow try/catch. And it implies an index too which will help performance
gbn
2010-10-22 06:59:06
A:
One technique would be to add a unique constraint on the column. Always insert a record and handle the failure case where that id already existed in the table.
Ozten
2010-10-22 05:10:29