简体   繁体   English

SQL删除所有约束Azure友好

[英]SQL Remove All Constraints Azure Friendly

I'm doing some DB Admin with an Azure database and I need to perform queries like removing all constraints in the database. 我正在使用Azure数据库做一些数据库管理员,我需要执行查询,例如删除数据库中的所有约束。

sp_MSForEachTable is not available when working with Azure databases, so I'm working on a different way to do it. 使用Azure数据库时,sp_MSForEachTable不可用,因此我正在采用不同的方法来执行此操作。

I found a snippet that drops all tables here: http://edspencer.me.uk/2013/02/25/drop-all-tables-in-a-sql-server-database-azure-friendly/ and tried modifying it to remove all constraints like I need to and came up with this result: 我找到了一个丢弃所有表格的片段: 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

It repeatedly tries to operate on the first item in the database, which would work fine if you were dropping everything but I need to loop through each table and disable its constraint like sp_MSForEachTable does. 它反复尝试对数据库中的第一项进行操作,如果你放弃了所有内容,它将正常工作,但我需要循环遍历每个表并禁用它的约束,如sp_MSForEachTable。

Any tips? 有小费吗? I've seen a few things here and there that claim to do this, but they're usually two or three page long scripts that do a lot of other stuff and they make my brain hurt. 我已经在这里和那里看到了一些声称要这样做的东西,但它们通常是两到三页长的脚本,它们会做很多其他的事情而且会让我的大脑受到伤害。

UPDATE UPDATE

still working on that query, it seems like something to this end might work better but still no dice: 仍在处理该查询,看起来这样的事情可能会更好但仍然没有骰子:

declare @constraintOff nvarchar(2000)
SELECT @constraintOff=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] ' + 'NOCHECK CONSTRAINT all')
FROM INFORMATION_SCHEMA.TABLES
exec (@constraintOff)
PRINT @constraintOff

This one still only operates on one table, but at least it's not an infinite loop :) 这个仍然只在一个表上运行,但至少它不是一个无限循环:)

While this link is for Amazon RDS, it does provide specific code to disable constraints without sp_MSForEachTable 虽然此链接适用于Amazon RDS,但它确实提供了特定的代码来禁用没有sp_MSForEachTable约束

Importing and Exporting SQL Server Data 导入和导出SQL Server数据

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

Extended the script to deal with tables in different schemas, also corrected the above script that is not disabling checks: 扩展脚本以处理不同模式中的表,还纠正了上述不禁用检查的脚本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM