Thursday, August 30, 2012

T-SQL - Coding - check table column exists in the database

Option-01: using SYS.COLUMNS 


IF EXISTS(SELECT * FROM SYS.COLUMNS WHERE NAME = N'account_id'  
            AND OBJECT_ID = OBJECT_ID(N'account'))
BEGIN
PRINT 'EXISTS';
END            
;

Option-02: using INFORMATION_SCHEMA.COLUMNS

IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_NAME = 'account' 
           AND  COLUMN_NAME = 'account_id')
BEGIN
PRINT 'EXISTS';
END            

1 comment:

Lori Weber said...

Greeat blog I enjoyed reading