How do I check if a column exists in SQL Server?

如何检查SQL Server 2000中是否存在列?


IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_NAME='tablename' AND COLUMN_NAME='columname' )

If col_length('table_name','column_name') is null
    select 0 as Present
ELSE
    select 1 as Present

Present will be 0, is there is no column_name present in table_name , otherwise 1

@CMS: I don't think that 'INFORMATION_SCHEMA.COLUMNS' have information about every table in DB. Because this didn't worked for me. But my answer did worked.


在查询分析器中,选择包含您需要检查该字段是否存在的表的数据库,并运行下面的查询。

SELECT count(*) AS [Column Exists] 
FROM SYSOBJECTS  
INNER JOIN SYSCOLUMNS ON SYSOBJECTS.ID = SYSCOLUMNS.ID 
WHERE 
 SYSOBJECTS.NAME = 'myTable' 
 AND SYSCOLUMNS.NAME = 'Myfield'
链接地址: http://www.djcxy.com/p/94408.html

上一篇: 如何删除列当且仅当它存在于9+版本的postgres中

下一篇: 如何检查SQL Server中是否存在列?