Monday, 3 July 2017

Add Column From Existing Table With 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

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...