Tuesday, January 26, 2010

TSQL object_id replaces if exists for object creation

Database object creation and scripting has a new friend. Enter the object_id() function. I have been using it instead of the old

if exists (select somthing with a name)

drop .....

create .....

Use it like so:

if object_id('fully qualified object name','xtype from sysobjects') is not null
drop object

Definately a little cleaner that the if exists. look it up in BOL.

Please note the remark in BOL.

When a temporary table name is specified, the database name must come before the temporary table name, unless the current database is tempdb. For example: SELECT OBJECT_ID('tempdb..#mytemptable').

I find it easier to use in my deployment scripts as well.

Labels:

0 Comments:

Post a Comment

<< Home