Monthly archives: March 2007

SQL: if exists

I collected these SQL scripts so that whenever I want to check the existency of a table, view, stored procedure, or function in a database, all I have to do is to copy and paste them to the Query Analyzer. FYI, These scripts are only used for SQL Server :) . Hope it’s useful!

Check if the table exists in a database:

1
2
3
4
5
6
7
8
9
10
IF EXISTS
(
  SELECT * FROM dbo.sysobjects
  WHERE id = object_id(N'[dbo].[enterTableNameHere]')
         AND OBJECTPROPERTY(id, N'IsUserTable') = 1
)
 
DROP TABLE [dbo].[enterTableNameHere]
 
GO

Continue reading