DECLARE @TBL1 TABLE (
rowNum INT identity(1, 1)
,table_name NVARCHAR(200)
,column_name NVARCHAR(100)
)
DECLARE @counter1 INT
,@count1 INT
INSERT INTO @TBL1
SELECT OBJECT_NAME(OBJECT_ID)
,OBJECT_NAME(parent_object_id)
FROM sys.objects
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT')
-- type_desc IN
('PRIMARY_KEY_CONSTRAINT') to delete primary key from all table in a db
SELECT @count1 = @@rowcount
,@counter1 = 1
SELECT *
FROM @TBL1
WHILE (@counter1 <= @count1)
BEGIN
DECLARE @tblName1 NVARCHAR(100)
DECLARE @clmName1 NVARCHAR(100)
,@sql11 NVARCHAR(256)
SELECT @clmName1 =
table_name
,@tblName1 =
column_name
FROM @TBL1
WHERE rowNum =
@counter1
SET @sql11 = 'ALTER TABLE ' +
@tblName1 + ' DROP
CONSTRAINT ' + @clmName1
EXEC (@sql11)
SET @counter1 =
@counter1 + 1
END
DECLARE @TBL TABLE (
rowNum INT identity(1, 1)
,table_name NVARCHAR(100)
,column_name NVARCHAR(100)
)
DECLARE @counter INT
,@count INT
INSERT INTO @TBL
SELECT object_name(object_id)
,NAME
FROM sys.columns
WHERE is_identity = 1
AND NAME NOT LIKE 'queuing%'
SELECT @count = @@rowcount
,@counter = 1
SELECT *
FROM @TBL
WHILE (@counter <= @count)
BEGIN
DECLARE @tblName NVARCHAR(100)
DECLARE @clmName NVARCHAR(100)
,@sql1 NVARCHAR(256)
SELECT @tblName =
table_name
,@clmName =
column_name
FROM @TBL
WHERE rowNum =
@counter
SET @sql1 = 'ALTER TABLE ' +
@tblName + ' ALTER
COLUMN ' + @clmName + ' BIGINT not null'
EXEC (@sql1)
PRINT @sql1
SET @counter =
@counter + 1
END
EXEC sp_MSforeachtable 'ALTER TABLE ? ADD PRIMARY
KEY(AuditLogID) '
Tags
SQL SERVER