Tag Archives: SQL Server

How to check if a column exists in a SQL Server table

By | June 11, 2018

How to check if a column exists in a SQL Server table, Syntax: IF EXISTS(SELECT 1 FROM sys.columns WHERE Name = N’columnName’ AND Object_ID = Object_ID(N’schemaName.tableName’)) BEGIN — Column is exists in DB END Example: Let’s Check wether address column is present in Employee table or not. IF EXISTS(SELECT 1 FROM sys.columns WHERE Name =… Read More »

How to set a default value for an existing column in sql server

By | November 9, 2017

How to set a default value for an existing column in SQL server Syntax: ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL| NOT NULL} CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE} [WITH VALUES] For Example: ALTER TABLE EmployeeMaster ADD City NOT NULL CONSTRAINT EmpMaster_City DEFAULT ‘Pune’ Click here for more SQL Tutuorials