Option 1: Check if Column Exists in Current Database
IF EXISTS
( SELECT *FROM sys.columnsWHERE Name = N'NameofColumn')BEGIN
SELECT 'Column Exists'-- add other logicEND
ELSE
BEGIN
SELECT 'Column Does NOT Exists'-- add other logicEND
Option 2: Check if Column Exists in Current Database in Specific Table
IF EXISTS
( SELECT *FROM sys.columnsWHERE Name = N'NameofColumn'AND OBJECT_ID = OBJECT_ID(N'tableName'))BEGIN
SELECT 'Column Exists'-- add other logicEND
ELSE
BEGIN
SELECT 'Column Does NOT Exists'-- add other logicEND
Additional Reference:
SQL SERVER – Query to Find Column From All Tables of Database
Reference: Pinal Dave (http://blog.SQLAuthority.com)
No comments:
Post a Comment