Monday, September 24, 2012

Validating Unique Columnname Across Whole Database

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

Reference: Pinal Dave (http://blog.SQLAuthority.com)

No comments:

Post a Comment