Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

Easily detect if a table or other object exists

Jun 26 2012 12:00AM by ErikEckhardt   

I recently saw the following code:

IF  EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[TableName]')
AND type in (N'U'))
   DROP TABLE [dbo].[TableName];

However, this is more complicated than needed. You can just do this:

IF Object_ID('dbo.TableName', 'U') IS NOT NULL DROP TABLE dbo.TableName;

The second parameter of Object_ID accepts any value from the xtype column of the sysobjects table. U is for user table.

To check for temp tables and temp stored procedures, do this:

IF Object_ID('tempdb.dbo.#TableName', 'U') IS NOT NULL DROP TABLE #TableName;
Read More..   [32134 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


ErikEckhardt
65 · 3% · 887
14
 
6
 
11
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

2  Comments  

  • Wow..This is more shorter way. I don't like to write IF-ELSE statements. I feel like writing procedural code while coding IF-ELSE.

    commented on Jun 27 2012 6:04AM
    Ramireddy
    2 · 41% · 12972
  • Thats a nice tip.. Thanks..

    http://letslearnssis.blogspot.com/

    commented on Jun 28 2012 7:19AM
    qutubmumbai
    2269 · 0% · 5

Your Comment


Sign Up or Login to post a comment.

"Easily detect if a table or other object exists" rated 5 out of 5 by 14 readers
Easily detect if a table or other object exists , 5.0 out of 5 based on 14 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]