SQL 删除所有限制 Azure 友好
SQL Remove All Constraints Azure Friendly
我正在使用 Azure 数据库执行一些数据库管理,我需要执行查询,例如删除数据库中的所有约束。
sp_MSForEachTable 在使用 Azure 数据库时不可用,因此我正在研究一种不同的方法。
我发现了一个片段,它把所有 table 都放在这里:http://edspencer.me.uk/2013/02/25/drop-all-tables-in-a-sql-server-database-azure-friendly/ 并尝试修改它以删除我需要的所有约束,并得出了这个结果:
while(exists(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME != '__MigrationHistory'))
begin
PRINT ('Disabling' + TABLE_NAME)
declare @constraintOff nvarchar(2000)
SELECT TOP 1 @constraintOff=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] ' + 'NOCHECK CONSTRAINT all')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME != '__MigrationHistory'
exec (@constraintOff)
PRINT @constraintOff
end
它反复尝试对数据库中的第一项进行操作,如果您删除所有内容,这会很好地工作,但我需要遍历每个 table 并像 sp_MSForEachTable 那样禁用其约束.
有什么建议吗?我在这里和那里看到了一些声称可以做到这一点的东西,但它们通常是两三页长的脚本,做很多其他的事情,它们让我的大脑受伤。
更新
仍在处理该查询,似乎为此目的可能会更好但仍然没有骰子:
declare @constraintOff nvarchar(2000)
SELECT @constraintOff=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] ' + 'NOCHECK CONSTRAINT all')
FROM INFORMATION_SCHEMA.TABLES
exec (@constraintOff)
PRINT @constraintOff
这个仍然只在一个table上运行,但至少它不是一个无限循环:)
虽然此 link 适用于 Amazon RDS,但它确实提供了特定代码来禁用没有约束的约束 sp_MSForEachTable
Importing and Exporting SQL Server Data
-- Manually specify database name - a safeguard in case you paste this into the wrong SSMS window.
USE [staging]
-- Change this line if you want to enable (1) or disable constraints:
DECLARE @enable_constraints bit = 0
--Don't change anything below this line.
DECLARE @schema_name SYSNAME
DECLARE @table_name SYSNAME
DECLARE table_cursor CURSOR FOR
SELECT
schemas.name,
tables.name
FROM
sys.tables
INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id
OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name
DECLARE @cmd varchar(200)
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd = 'ALTER TABLE ' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name) + ' '
SET @cmd = @cmd + (CASE WHEN @enable_constraints = 1 THEN 'CHECK' ELSE 'NOCHECK' END) + ' CONSTRAINT ALL'
PRINT @cmd
EXEC( @cmd )
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name
END
CLOSE table_cursor
DEALLOCATE table_cursor
扩展脚本以处理不同模式中的表,还更正了上面未禁用检查的脚本:
-- DISABLE ALL CONSTRAINTS
DECLARE @table_name SYSNAME;
DECLARE @schema_name SYSNAME;
DECLARE @cmd NVARCHAR(MAX);
DECLARE table_cursor CURSOR FOR
SELECT s.name, t.name
FROM sys.tables t
join sys.schemas s on t.schema_id = s.schema_id
OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @cmd = 'ALTER TABLE '+QUOTENAME(@schema_name)+'.'+QUOTENAME(@table_name)+' NOCHECK CONSTRAINT ALL';
EXEC (@cmd);
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
END
CLOSE table_cursor;
DEALLOCATE table_cursor;
-- enable all constraints
DECLARE table_cursor CURSOR FOR
SELECT s.name, t.name
FROM sys.tables t
join sys.schemas s on t.schema_id = s.schema_id
OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @cmd = 'ALTER TABLE '+QUOTENAME(@schema_name)+'.'+QUOTENAME(@table_name)+' CHECK CONSTRAINT ALL';
EXEC (@cmd);
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
END
CLOSE table_cursor;
DEALLOCATE table_cursor;
我正在使用 Azure 数据库执行一些数据库管理,我需要执行查询,例如删除数据库中的所有约束。
sp_MSForEachTable 在使用 Azure 数据库时不可用,因此我正在研究一种不同的方法。
我发现了一个片段,它把所有 table 都放在这里:http://edspencer.me.uk/2013/02/25/drop-all-tables-in-a-sql-server-database-azure-friendly/ 并尝试修改它以删除我需要的所有约束,并得出了这个结果:
while(exists(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME != '__MigrationHistory'))
begin
PRINT ('Disabling' + TABLE_NAME)
declare @constraintOff nvarchar(2000)
SELECT TOP 1 @constraintOff=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] ' + 'NOCHECK CONSTRAINT all')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME != '__MigrationHistory'
exec (@constraintOff)
PRINT @constraintOff
end
它反复尝试对数据库中的第一项进行操作,如果您删除所有内容,这会很好地工作,但我需要遍历每个 table 并像 sp_MSForEachTable 那样禁用其约束.
有什么建议吗?我在这里和那里看到了一些声称可以做到这一点的东西,但它们通常是两三页长的脚本,做很多其他的事情,它们让我的大脑受伤。
更新
仍在处理该查询,似乎为此目的可能会更好但仍然没有骰子:
declare @constraintOff nvarchar(2000)
SELECT @constraintOff=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] ' + 'NOCHECK CONSTRAINT all')
FROM INFORMATION_SCHEMA.TABLES
exec (@constraintOff)
PRINT @constraintOff
这个仍然只在一个table上运行,但至少它不是一个无限循环:)
虽然此 link 适用于 Amazon RDS,但它确实提供了特定代码来禁用没有约束的约束 sp_MSForEachTable
Importing and Exporting SQL Server Data
-- Manually specify database name - a safeguard in case you paste this into the wrong SSMS window.
USE [staging]
-- Change this line if you want to enable (1) or disable constraints:
DECLARE @enable_constraints bit = 0
--Don't change anything below this line.
DECLARE @schema_name SYSNAME
DECLARE @table_name SYSNAME
DECLARE table_cursor CURSOR FOR
SELECT
schemas.name,
tables.name
FROM
sys.tables
INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id
OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name
DECLARE @cmd varchar(200)
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd = 'ALTER TABLE ' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name) + ' '
SET @cmd = @cmd + (CASE WHEN @enable_constraints = 1 THEN 'CHECK' ELSE 'NOCHECK' END) + ' CONSTRAINT ALL'
PRINT @cmd
EXEC( @cmd )
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name
END
CLOSE table_cursor
DEALLOCATE table_cursor
扩展脚本以处理不同模式中的表,还更正了上面未禁用检查的脚本:
-- DISABLE ALL CONSTRAINTS
DECLARE @table_name SYSNAME;
DECLARE @schema_name SYSNAME;
DECLARE @cmd NVARCHAR(MAX);
DECLARE table_cursor CURSOR FOR
SELECT s.name, t.name
FROM sys.tables t
join sys.schemas s on t.schema_id = s.schema_id
OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @cmd = 'ALTER TABLE '+QUOTENAME(@schema_name)+'.'+QUOTENAME(@table_name)+' NOCHECK CONSTRAINT ALL';
EXEC (@cmd);
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
END
CLOSE table_cursor;
DEALLOCATE table_cursor;
-- enable all constraints
DECLARE table_cursor CURSOR FOR
SELECT s.name, t.name
FROM sys.tables t
join sys.schemas s on t.schema_id = s.schema_id
OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @cmd = 'ALTER TABLE '+QUOTENAME(@schema_name)+'.'+QUOTENAME(@table_name)+' CHECK CONSTRAINT ALL';
EXEC (@cmd);
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name;
END
CLOSE table_cursor;
DEALLOCATE table_cursor;