Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there an easy way to find if table exists in a database?

Status
Not open for further replies.

Kenos

Programmer
Jul 21, 2002
28
0
0
US
Using the Master database is there a way to return a value if a table exists in a different database.
Any help would be great.

Currently I am trying to get it this way.

DECLARE @vCharDatabaseName varchar(255)
SET @vCharDatabaseName = 'MyDatabase'
/* @vCharDatabaseName is passed into the SP */


DECLARE @TableName varchar(255)

SET @TableName = '[' + @vCharDatabaseName +'].[dbo].[DumpTable]'

If EXISTS(select * FROM SYSDATABASES, sysobjects WHERE SYSDATABASES.NAME =@vCharDatabaseName AND (sysobjects.id = object_id(@TableName))and OBJECTPROPERTY(id, N'IsUserTable') = 1)

BEGIN
TRUNCATE table @TableName
Print 'TRUNCATE DumpTable'
END
Else
Print 'No Action Taken'

 
does this help ?
Code:
sp_MSforeachdb 'select * from information_schema.tables'

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top