SQL删除所有约束Azure友好

时间:2015-04-24 15:18:31

标签: sql tsql azure azure-sql-database

我正在使用Azure数据库进行一些数据库管理,我需要执行查询,例如删除数据库中的所有约束。

使用Azure数据库时,sp_MSForEachTable不可用,因此我正在采用不同的方法来执行此操作。

我找到了一个代码片段,它会删除所有表格: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

它反复尝试对数据库中的第一个项进行操作,如果你放弃了所有内容,它将正常工作,但我需要循环遍历每个表并禁用它的约束,如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

这个仍然只在一个表上运行,但至少它不是一个无限循环:)

2 个答案:

答案 0 :(得分:6)

虽然此链接适用于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

答案 1 :(得分:2)

扩展脚本以处理不同模式中的表,还纠正了上述不禁用检查的脚本:

    -- 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;