SQL Server根据列名查表
select a.name, b.name from syscolumns a, sysobjects b where a.name = ‘XXXX‘ and a.id = b.id and b.xtype = ‘u‘
--判断用户表是否存在
if objectproperty(object_id( ‘要判断的表名 ‘),
‘IsUserTable ‘)=1
print ‘存在 ‘
--判断指定的存储过程是否存在
if objectproperty(object_id( ‘要判断的表名 ‘),
‘IsProcedure ‘)=1
print ‘存在
‘
--判断数据库是否存在
IF EXISTS (SELECT * FROM MASTER..sysdatabases WHERE NAME = ‘库名‘)
PRINT ‘exists ‘
else
PRINT ‘not exists‘
-- 判断要创建的表名是否存在
IF EXISTS (Select * From sysObjects Where Name =‘表名‘ And Type In (‘S‘,‘U‘))
PRINT ‘exists‘
ELSE
PRINT ‘not exists‘
GO
--判断要创建临时表是否存在
If Object_Id( ‘Tempdb.dbo.#Test‘) Is Not NULL--#Test 为临时表名
Begin
print ‘存在 ‘
End
Else
Begin
print ‘不存在 ‘
End
---------------
-- 判断要创建的存储过程名是否存在
IF EXISTS (Select * From sysObjects Where Name =‘存储过程名‘ And Type In (‘S‘,‘P‘))
PRINT ‘exists‘
ELSE
PRINT ‘not exists‘
GO
-- 判断列名是否存在
IF EXISTS (SELECT O.NAME AS 表名,C.NAME AS 列名
FROM SYSOBJECTS O INNER JOIN SYSCOLUMNS C ON O.ID=C.ID
WHERE O.NAME= ‘表名‘ AND C.NAME = ‘列名‘)
SELECT ‘EXISTS‘
ELSE
SELECT ‘NOT EXISTS‘