Solution:
We will get the list of Primary Key Constraint by using System Views. Once we have the information we will use the Cursor to loop through list of Primary Key Constraint and drop them by using below script.
The general script to drop Primary Key Constraint is
Alter Table SchemaName.TableName
Drop Constraint ConstraintName
Here is our script to drop all the Primary Key Constraints from a Database
USE <<YourDatabaseName>>
GO
--Declare Variables
DECLARE @DatabaseName AS VARCHAR(255)
DECLARE @SchemaName AS VARCHAR(255)
DECLARE @TableName AS VARCHAR(255)
DECLARE @ColumnName AS VARCHAR(255)
DECLARE @ConstraintName AS VARCHAr(255)
DECLARE CUR CURSOR
FOR
--Get Primary Key Constraint
Select
TC.Table_Catalog as DatabaseName,
TC.Table_Schema AS TableSchema,
TC.Table_Name AS TableName,
CCU.Column_Name AS ColumnName,
TC.Constraint_Name AS ConstraintName
From
information_Schema.Table_Constraints TC
INNER JOIN
Information_Schema.constraint_column_usage CCU
on TC.Constraint_Name=CCU.Constraint_Name
and TC.Table_Name=CCU.Table_Name
where
Constraint_Type='PRIMARY KEY'
OPEN Cur
FETCH NEXT
FROM Cur
INTO @DatabaseName,@SchemaName,@TableName,@ColumnName,
@ConstraintName
WHILE @@FETCH_STATUS = 0
BEGIN
--Build dynamic sql for each database
DECLARE @SQL VARCHAR(MAX) = NULL
SET @SQL ='Alter table ['
SET @SQL+=@SchemaName+'].['+@TableName+']'
SET @SQL+=' Drop Constraint ['
SET @SQL+=@ConstraintName+']'
PRINT @SQL
EXEC (@SQL)
FETCH NEXT
FROM Cur
INTO @DatabaseName,@SchemaName,@TableName,@ColumnName,
@ConstraintName
END
CLOSE Cur
DEALLOCATE Cur
We will get the list of Primary Key Constraint by using System Views. Once we have the information we will use the Cursor to loop through list of Primary Key Constraint and drop them by using below script.
The general script to drop Primary Key Constraint is
Alter Table SchemaName.TableName
Drop Constraint ConstraintName
Here is our script to drop all the Primary Key Constraints from a Database
USE <<YourDatabaseName>>
GO
--Declare Variables
DECLARE @DatabaseName AS VARCHAR(255)
DECLARE @SchemaName AS VARCHAR(255)
DECLARE @TableName AS VARCHAR(255)
DECLARE @ColumnName AS VARCHAR(255)
DECLARE @ConstraintName AS VARCHAr(255)
DECLARE CUR CURSOR
FOR
--Get Primary Key Constraint
Select
TC.Table_Catalog as DatabaseName,
TC.Table_Schema AS TableSchema,
TC.Table_Name AS TableName,
CCU.Column_Name AS ColumnName,
TC.Constraint_Name AS ConstraintName
From
information_Schema.Table_Constraints TC
INNER JOIN
Information_Schema.constraint_column_usage CCU
on TC.Constraint_Name=CCU.Constraint_Name
and TC.Table_Name=CCU.Table_Name
where
Constraint_Type='PRIMARY KEY'
OPEN Cur
FETCH NEXT
FROM Cur
INTO @DatabaseName,@SchemaName,@TableName,@ColumnName,
@ConstraintName
WHILE @@FETCH_STATUS = 0
BEGIN
--Build dynamic sql for each database
DECLARE @SQL VARCHAR(MAX) = NULL
SET @SQL ='Alter table ['
SET @SQL+=@SchemaName+'].['+@TableName+']'
SET @SQL+=' Drop Constraint ['
SET @SQL+=@ConstraintName+']'
PRINT @SQL
EXEC (@SQL)
FETCH NEXT
FROM Cur
INTO @DatabaseName,@SchemaName,@TableName,@ColumnName,
@ConstraintName
END
CLOSE Cur
DEALLOCATE Cur
No comments:
Post a Comment