Summary judgments sql knowledge database, table, column exists

2011-05-17  来源:本站原创  分类:Database  人气:74 

Determine the current name of the database

Code:

DECLARE @ maDbName SYSNAME
IF (DB_NAME () LIKE N '% Test')
BEGIN
SET @ maDbName = N'MemberArchiveTest '
END
ELSE
BEGIN
SET @ maDbName = N'MemberArchive '
END

Code Interpretation: If the test at the end of the current database is based on the corresponding set

Determine whether there is a library database:

EXISTS (SELECT 1 FROM master.dbo.sysdatabases WHERE [name] = databasename)

Tables to determine whether there is a

select 1 from sys.tables where OBJECT_ID = OBJECT_ID ('TableName', N'U ')
and EXISTS (SELECT 1 FROM master.dbo.sysdatabases WHERE [name] = 'DatabaseName')

Judge of a particular table in a field exists:

EXISTS (SELECT 1 FROM sys.columns WHERE [object_id] = OBJECT_ID (N'dbo. Tr_Role ', N'U')
AND [name] = N'MembershipNumber ')

相关文章