Saturday, February 25, 2012

Checking whether the table has a column with the specified name

Hi,

I have a table as follows

Table

{

Category1,

Category2,

Category3

}

I wanted to write a query to check whether the column with the name 'Category3' exists in the table.

Can anyone please let me know how to do this

Best Regards and Thanks

~Mohan Babu

2005?

Check out the sys.columns table

|||

Code Snippet

if Exists(

select * from sys.columns where Name = N'Category3'

and Object_ID = Object_ID(N'<TableName>')

)

begin

--some code for column exists condition

end

else

begin

--some code for column does not exist condition

end

|||Hi,

If you are using sql2005 then u can make use of rusag2's querry else if you are using sql2000 the u can make use of the following querry:

Code Snippet

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


No comments:

Post a Comment