How to check if a table exists in SQL Server 2000/2005 using SQL Statement.
Here are two possible ways of doing it. Which one among the two is the standard/best way of doing it?
First Method:
IF EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='tblStudent'
)
BEGIN
-- do needful
END
Second Method:
IF OBJECT_ID(N'tblStudent',N'U') IS NOT NULL
BEGIN
SELECT *FROM tblStudent
END
Also to check for a temporary Table, we have to query the tempdb database and use a Like operator for the table name
SELECT *FROM tempdb.INFORMATION_SCHEMA WHERE TABLE_SCHEMA =' TheSchema' AND TABLE_Name LIKE '#MyTemp%'
another method
IF OBJECT_ID('tempdb..#Temp2') IS NOT NULL
BEGIN
SELECT *FROM #Temp2
END
No comments:
Post a Comment