How do I check if a column exists in SQL Server 2000?
问问题
36447 次
5 回答
38
IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename' AND COLUMN_NAME='columname' )
于 2008-10-24T05:46:40.023 回答
9
If col_length('table_name','column_name') is null
select 0 as Present
ELSE
select 1 as Present
Present 将为 0,如果 table_name 中没有 column_name,否则为 1
@CMS:我认为“INFORMATION_SCHEMA.COLUMNS”没有关于数据库中每个表的信息。因为这对我不起作用。但我的回答确实奏效了。
于 2010-08-31T21:02:55.870 回答
5
In query analyzer, select the Database that contains the table in which you need to check if the field exists or not and run the query below.
SELECT count(*) AS [Column Exists]
FROM SYSOBJECTS
INNER JOIN SYSCOLUMNS ON SYSOBJECTS.ID = SYSCOLUMNS.ID
WHERE
SYSOBJECTS.NAME = 'myTable'
AND SYSCOLUMNS.NAME = 'Myfield'
于 2008-10-24T06:03:31.457 回答
2
这应该做得很好:
if COLUMNPROPERTY(object_id('table_name'), 'column_name', 'ColumnId') is null
print 'doesn\'t exist'
else
print 'exists'
于 2011-04-29T15:02:40.137 回答
0
我不知道这个脚本是否可以在 sqlserver 2000 中工作,但在 2008 年可以工作:
SELECT COLUMNS.*
FROM INFORMATION_SCHEMA.COLUMNS COLUMNS, INFORMATION_SCHEMA.TABLES TABLES
WHERE COLUMNS.TABLE_NAME=TABLES.TABLE_NAME AND UPPER(COLUMNS.COLUMN_NAME)=UPPER('column_name')
于 2011-09-30T13:04:20.393 回答