Friday, April 3, 2009

SQL - Yet another thing about checking existence of a table in a MS SQL database

- The first method is checking specific table in sysobjects with
'U' value for xtype column which shows the object is a [user table].

USE AclMacGen
GO
IF EXISTS (
SELECT *
FROM sysobjects
WHERE
id = OBJECT_ID('[myDB].[dbo].[myTable]')
AND xtype = 'U'
)
BEGIN
SELECT 'It is EXIST'
END


- The second way is just by checking availability of specific
table as an object in the database.

IF OBJECT_ID('[myDB].[dbo].[myTable]') IS NOT NULL
BEGIN
SELECT 'It is EXIST'
END

Share/Bookmark

No comments: