Monday, June 16, 2014

Sql Check if Object Exists

Josh,

You know those situations where you want to drop and recreate something, or create something if it doesn't exist?  You can use the object_id function to check if an object exists in the database.

IF OBJECT_ID('MyTableName'IS NULL
    
PRINT 'Object Does not Exist'
ELSE
    PRINT 
'Object Exists'


Other object_id syntax options:

  • With server and schema: OBJECT_ID('MyServer.MySchema.MyTableName')
  • Just tables: OBJECT_ID('MyServer.MySchema.MyTableName''U')
Thanks to http://stackoverflow.com/a/5952674/1582172

No comments:

Post a Comment

Please only include comments that add to the substance of the Blog Post:
- Question or request for clarification
- Idea for improvement
- Identifying an issue

Please refrain from promotion.