Tuesday, March 12, 2013

T-SQL Library - Check if Column Exists Alter Table


Composing T-SQL to run over and over to check into source control for column change....

IF NOT EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = 'tbl_Provider_Concerns'
           AND  COLUMN_NAME = 'concern_status_id')
           BEGIN
ALTER TABLE tbl_Provider_Concerns
ADD concern_status_id INT
  END


ALTER TABLE tbl_Provider_Concerns
ADD CONSTRAINT FK_Status
FOREIGN KEY(concern_status_id)
REFERENCES tbl_Provider_Concern_Status(concern_status_id)
Post a Comment