删除约束,然后在SQL Server中删除表

时间:2013-04-08 06:56:48

标签: sql-server tsql stored-procedures

这对你来说听起来很简单,但不是第一次写

我有一个查询,使用以下查询删除表中的所有约束

SELECT @sql += N'
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + 
    ' DROP CONSTRAINT ' + QUOTENAME(name) + ';'
FROM sys.foreign_keys;

EXEC sp_executesql @sql;

因此,如果这是成功的(删除所有约束),我需要删除所有表。

否则显示有效的错误消息watever约束查询抛出。

如何在上述场景中添加条件?

1 个答案:

答案 0 :(得分:2)

您可能想要添加TRY....CATCH,还需要初始化变量@sql。您也可以考虑提高您的错误。


declare @sql nvarchar(max)
set @sql = ''
SELECT @sql += N'
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + 
    ' DROP CONSTRAINT ' + QUOTENAME(name) + ';' 
FROM sys.foreign_keys;
BEGIN TRY
EXEC sp_executesql @sql;
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage         NVARCHAR(4000)
DECLARE @ErrorState INT
DECLARE @ErrorSeverity INT --Obtain the error message, severity and state
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE()
--RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH
如果要在删除外键约束后立即删除表。


declare @sql nvarchar(max)
declare @sqlToDropTables nvarchar(max)
set @sqlToDropTables=''
set @sql = ''

SELECT @sqlToDropTables +=N' DROP TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
                                          + '.'
                                          + QUOTENAME(OBJECT_NAME(parent_object_id)) 
                                          + ';'
FROM sys.foreign_keys;

SELECT @sql += N'
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + 
    ' DROP CONSTRAINT ' + QUOTENAME(name) + ';' 
FROM sys.foreign_keys;

BEGIN TRY
EXEC sp_executesql @sql;
EXEC sp_executesql @sqlToDropTables
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage         NVARCHAR(4000)          
    DECLARE @ErrorState           INT          
    DECLARE @ErrorSeverity        INT 
    --Obtain the error message, severity and state      
    SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE()      
    --RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)         
END CATCH

添加更多代码,如果要清理所有表,请包含引用对象,即PK表。


declare @sql nvarchar(max)
declare @sqlToDropTables nvarchar(max)
set @sqlToDropTables=''
set @sql = ''

SELECT @sqlToDropTables +=N' DROP TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
                                          + '.'
                                          + QUOTENAME(OBJECT_NAME(parent_object_id)) 
                                          + ';'
                                          + 'DROP TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(referenced_object_id))
                                                          + '.'
                                                          + QUOTENAME(OBJECT_NAME(referenced_object_id)) 
                                                          + ';'

FROM sys.foreign_keys;

SELECT @sql += N'
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + 
    ' DROP CONSTRAINT ' + QUOTENAME(name) + ';' 
FROM sys.foreign_keys;

BEGIN TRY
EXEC sp_executesql @sql;
EXEC sp_executesql @sqlToDropTables
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage         NVARCHAR(4000)          
    DECLARE @ErrorState           INT          
    DECLARE @ErrorSeverity        INT 
    --Obtain the error message, severity and state      
    SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE()      
    --RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)         
END CATCH