Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 733
user31071 Avatar asked Oct 24 '08 05:10

user31071


People also ask

How do I find the columns in a SQL table?

In a query editor, if you highlight the text of table name (ex dbo. MyTable) and hit ALT + F1 , you'll get a list of column names, type, length, etc.

How do you check multiple column exists in table in SQL Server?

Change equal operator to IN operator then Name, some like this: SELECT * FROM sys. columns WHERE Name IN ('columnName_1','columnName_2') AND OBJECT_ID = OBJECT_ID('tableName').

How do I find column names in SQL?

In SQL Server, you can select COLUMN_NAME from INFORMATION_SCHEMA. COLUMNS .

How do you check if data exists in a table in SQL Server?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.


2 Answers

IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_NAME='tablename' AND COLUMN_NAME='columname' ) 
like image 132
Christian C. Salvadó Avatar answered Sep 28 '22 02:09

Christian C. Salvadó


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

@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.

like image 20
Deepak Yadav Avatar answered Sep 28 '22 01:09

Deepak Yadav