SQL Server Performance Forum – Threads Archive
Checking if Select returns anything
Hi All,I am working on some new procedures that will need to perform some actions based on whether any results are returned/not returned by a Select Statement and was wondering what the best method to use is. Some code i have found from the previous DBA used: SELECT * FROM table1 WHERE Col1 = ‘SomeValue’
IF @@ROWCOUNT <> 0
BEGIN
blah, blah
END I am considering using:
IF (SELECT TOP 1 Col1 FROM Table1 WHERE Col1 = ‘SomeValue’) IS NOT NULL
BEGIN
blah, blah
END Are there other techniques i could use? From a performance point of view, what would be the best method?? Thanks,
Ben
Try IF EXISTS (select id from table where Col1=’SomeValue’)
BEGIN END
]]>