How to check if a column exists in SQL Server table -


i need add specific column if not exist. have this, returns false:

if exists(select *             information_schema.columns            table_name = 'mytablename'                  , column_name = 'mycolumnname')  

how can check if column exists in table of sql server database?

sql server 2005 onwards:

if exists(select 1 sys.columns            name = n'columnname'           , object_id = object_id(n'schemaname.tablename')) begin     -- column exists end 

martin smith's version shorter:

if col_length('schemaname.tablename', 'columnname') not null begin     -- column exists end 

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -