Alter Table Add Column

Adding a column to a table in SQL Server is done using the ALTER TABLE tablename ADD command. When adding columns you can specify all the same settings available when creating a table. In the example below, we will create a small sample table, then add columns using the ALTER TABLE command. Multiple columns can be specificied by using the syntax below. First issue an alter table command then add the column name, data type, nullable, and default value. IF OBJECT_ID(’tempdb..#employees’) IS NOT NULL BEGIN     DROP TABLE #employees END CREATE TABLE #employees (     EmployeeID INT IDENTITY (1,1) CONSTRAINT PK_employee NOT NULL PRIMARY KEY CLUSTERED     ,FirstName VARCHAR(50) ) GO INSERT INTO #employees (FirstName) VALUES (’John’);   ALTER TABLE #employees ADD      LastName VARCHAR(50) NULL     ,SSN VARCHAR(9) NULL CONSTRAINT ssn_unique UNIQUE     ,IsTerminated bit NOT NULL DEFAULT 0     ,DateAdded datetime NOT NULL DEFAULT GETDATE()     ,DateModified DATETIME NULL DEFAULT GETDATE()     ,Comments VARCHAR(255) SPARSE NULL — SQL Server 2008 sparse column     SELECT * FROM #employees Also note, when you add columns with a default value, the value will only get populated when you specify the column as NOT NULL. Otherwise, NULL values will populate the column initially. However once the column is created, subsequent insert statements will populate the default value. In the above example, DateAdded is initially populated whereas DateModified is only populated after a row is inserted.

Continue reading ...

Dynamically Drop Table Constraints

System generated constraints take on a naming convention of their own. Unfortunately the naming convention in production is rarely the same name in the uncontrolled environments. Using this script, you can dynamically drop all system generated constraints. It doesn’t go as far are re-creating them, however it’s a start. Just change the values of the @TableSchema and TableName variables below: DECLARE @TableName      VARCHAR(100) DECLARE @TableSchema    VARCHAR(100) DECLARE @CountConst     INT DECLARE @DEFAULT        sysname DECLARE @SQLDropMe      VARCHAR(MAX) DECLARE @ColumnNames    VARCHAR(MAX) SET @TableSchema = ‘dbo’ SET @TableName = ’employees’ ——————————————— Store Existing Column Names SET @ColumnNames = SUBSTRING((SELECT             ‘,’ + r.COLUMN_NAME             FROM(                 SELECT COLUMN_NAME                 FROM INFORMATION_SCHEMA.COLUMNS                 WHERE TABLE_SCHEMA = @TableSchema AND                     TABLE_NAME = @TableName             ) r             FOR XML PATH(”)   ), 2, 8000) ——————————————— /Store Existing Column Names ——————————————— Insert contents into temp table EXEC (’SELECT * INTO ‘ + @TableSchema + ‘.tmp01_’ + @TableName + ‘ FROM ‘ + @TableSchema + ‘.’ + @TableName) ——————————————— /Insert contents into temp table ——————————————— Drop all the constraints DECLARE @TableConstraints TABLE (     ID              INT     IDENTITY(1,1)   […]

Continue reading ...

Featured Articles

 Site Author

  • Thanks for visiting!