USE OF OBJECTPROPERTY TO FIND WHETHER AN OBJECT IS A TABLE NOT OR NOT

Eg if the table name is valid

DECLARE @TableName as varchar(40)
SET @TableName = ‘Employee’
IF OBJECTPROPERTY (OBJECT_ID(@TableName),’ISTABLE’) = 1
BEGIN
PRINT ‘Employee is a table.’
END
ELSE IF OBJECTPROPERTY (OBJECT_ID(@TableName),’ISTABLE’) = 0
BEGIN
PRINT ‘Employee is not a table.’
END
ELSE IF OBJECTPROPERTY (OBJECT_ID(@TableName),’ISTABLE’) IS NULL
BEGIN
PRINT ‘Employee is not a valid object.’;
END

Output
Employee is a table.

Eg if the table name is not valid

DECLARE @TableName as varchar(40)
SET @TableName = ‘TempTable’
IF OBJECTPROPERTY (OBJECT_ID(@TableName),’ISTABLE’) = 1
BEGIN
PRINT ‘TempTable is a table.’
END
ELSE IF OBJECTPROPERTY (OBJECT_ID(@TableName),’ISTABLE’) = 0
BEGIN
PRINT ‘TempTable is not a table.’
END
ELSE IF OBJECTPROPERTY (OBJECT_ID(@TableName),’ISTABLE’) IS NULL
BEGIN
PRINT ‘TempTable is not a valid object.’;
END

Output
TempTable is not a valid object.

Pawan Kumar
Pawankkmr@hotmail.com

Advertisements