Groups | Blog | Home
all groups > sql server (alternate) > december 2006 >

sql server (alternate) : if exists condition..


fireball
12/20/2006 6:17:24 PM
which is more proper:
if DB_ID(@db) IS NOT NULL
or
IF EXISTS (SELECT name FROM sys.databases WHERE name = @db)
?




regards

Erland Sommarskog
12/20/2006 10:53:25 PM
fireball (fireball@onet.kropka.eu) writes:
[quoted text, click to view]

Whichever you fancy. I would probably write the second nine times out
of ten, but that it would only be because db_id() would not come in
my mind. And a nice characteristic of the first, is that it works on
SQL 2000 as well.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
fireball
12/21/2006 10:20:43 AM
[quoted text, click to view]
[...]

thank you.
so you would say, first solution is not less professional at all then the
second one.. - right?

Erland Sommarskog
12/21/2006 1:02:30 PM
fireball (fireball@onet.kropka.eu) writes:
[quoted text, click to view]

Yes, both are perfectly OK, and which you pick is a matter of taste or
the flux of the moment.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Tony Rogerson
12/21/2006 5:07:41 PM
The first one; its more portable between versions of SQL Server, the second
only works in SQL 2005 and upwards.

--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials


[quoted text, click to view]

AddThis Social Bookmark Button