Sunday, December 5, 2010

New NOT NULL column on existing table

Script:


if not exists ( select 1 from sys.tables t
join sys.columns c on c.object_id = t.object_id
where t.name = 'PushInterfaceRESTLog' and c.name = 'UserType')
BEGIN
ALTER TABLE PushInterfaceRESTLog
ADD UserType varchar(100) NULL
END
GO

if exists ( select 1 from sys.tables t
join sys.columns c on c.object_id = t.object_id
where t.name = 'PushInterfaceRESTLog' and c.name = 'UserType')
BEGIN
UPDATE PushInterfaceRESTLog SET UserType = 'Vendor'

ALTER TABLE PushInterfaceRESTLog
ALTER COLUMN UserType varchar(100) NOT NULL
END
GO

No comments: