Groups | Blog | Home
all groups > sql server programming > february 2007 >

sql server programming : How to check for existence of a record?


Hari Prasad
2/22/2007 9:58:53 PM
Hello,

Use IF EXISTS


Sample:-

IF EXISTS(SELECT 1 FROM TableName WHERE ID= @ID)
BEGIN
SELECT 'This record already exists!'
END
ELSE
BEGIN
INSERT into TableName(Col1,Col2) VALUES(@ID)
END

Thanks
Harik

[quoted text, click to view]

bob
2/22/2007 10:50:04 PM
Hi,

Could someone please tell me the most efficient way to check for existence
of a record? I'd like to find out if there is one or more records in a
table matching certain criteria. I think I could do it with COUNT(*) or
SET ROW COUNT / SELECT combination. Is there a more efficient way of doing
it in SQL Server?

Thanks,
Bob

AddThis Social Bookmark Button