Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Check if record exists

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-02-09 : 20:04:20
Hello,

I need to find if a record with a given id exists:

IF EXISTS (SELECT title FROM dbo.Doc WHERE id = @id))

Do I really need to do SELECT title?

Can't I just do something like

IF EXISTS (SELECT NULL FROM dbo.Doc WHERE id = @id))

How should I do this?

Thanks,
Miguel

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-09 : 21:24:27
you can do both. But commonly, we will just select * from dbo.DOC whre id = @id


KH

Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-02-09 : 21:26:30
What you suggested will work (except for the syntax error), but the best way to do it is:
IF EXISTS ( SELECT * FROM dbo.Doc WHERE id = @id )

This allows SQL Server to chose the most efficient query plan available.


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -