Follow SQLMaster on Twitter
Welcome to SqlServer-QA.net Sign in | Help

TSQL to check whether the table has a column with the specified name?

Within SQL Server 2005: 

if Exists(

select * from sys.columns where Name = N'<ColumnName>'  and Object_ID = Object_ID(N'<TableName>')

)

begin

--write your own code print relevant code columns for existence

end

else

begin

--write your own code print relevant code columns for non-existence

end

For SQL Server 2000:

SELECT CASE WHEN ISNULL(COLUMN_NAME ,'')='' THEN 'NO' ELSE 'YES' END
FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME='VoucherSerial' AND TABLE_NAME='Vouchers'

Published Wednesday, September 19, 2007 3:51 PM by SQL Master

Comments

# TSQL to check whether the table has a column with the specified name?

Within SQL Server 2005: if Exists( select * from sys.columns where Name = N &#39;&lt;ColumnName&gt;&#39;

Wednesday, September 19, 2007 4:17 PM by Other SQL Server Blogs around the Web

# TSQL to check whether the table has a column with the specified name?

Within SQL Server 2005: if Exists( select * from sys.columns where Name = N &#39;&lt;ColumnName&gt;&#39;

Wednesday, September 19, 2007 4:37 PM by SSQA.net - SqlServer-QA.net
Anonymous comments are disabled