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

时间:2022-04-26 07:13:09

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

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

5 个解决方案

#1


36  

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

#2


8  

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

当前将为0,table_name中是否存在column_name,否则为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.

@CMS:我不认为'INFORMATION_SCHEMA.COLUMNS'有关于DB中每个表的信息。因为这对我不起作用。但我的回答确实有效。

#3


4  

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'

#4


1  

This should do nicely:

这应该做得很好:

if COLUMNPROPERTY(object_id('table_name'), 'column_name', 'ColumnId') is null
  print 'doesn\'t exist'
else
  print 'exists'

#5


0  

I don't know if this script will work in sqlserver 2000, but in 2008 works:

我不知道这个脚本是否可以在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')

#1


36  

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

#2


8  

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

当前将为0,table_name中是否存在column_name,否则为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.

@CMS:我不认为'INFORMATION_SCHEMA.COLUMNS'有关于DB中每个表的信息。因为这对我不起作用。但我的回答确实有效。

#3


4  

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'

#4


1  

This should do nicely:

这应该做得很好:

if COLUMNPROPERTY(object_id('table_name'), 'column_name', 'ColumnId') is null
  print 'doesn\'t exist'
else
  print 'exists'

#5


0  

I don't know if this script will work in sqlserver 2000, but in 2008 works:

我不知道这个脚本是否可以在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')