删除约束删除SQL中的行

时间:2017-10-23 16:38:20

标签: sql-server sql-server-2008

我在删除数据库中的所有约束时遇到问题。我有一张桌子tblBrowser。如果我运行此查询:

SELECT ID FROM tblBrowser

它返回1575337行。如果我然后运行此查询:

CREATE TABLE #tmp (ID INT IDENTITY (1, 1), tblName VARCHAR(256), ConstraintName VARCHAR(256))


-- Remove the indexes
INSERT INTO #tmp (tblName, ConstraintName)
    SELECT TABLE_NAME, CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE 
    WHERE 
    CONSTRAINT_CATALOG = 'MyDB'
    AND CONSTRAINT_NAME LIKE 'IX_%'


    DECLARE cur CURSOR FOR 
    SELECT tblName, ConstraintName
    FROM #tmp
    ORDEr BY ID

OPEN cur

DECLARE @Name VARCHAR(256)
DECLARE @Constraint VARCHAR(256)

FETCH NEXT FROM cur INTO @Name, @Constraint

WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT 'Dropping constraint: ' + @Constraint + ' from ' + @Name
    EXEC ('ALTER TABLE [dbo].[' + @Name + '] DROP CONSTRAINT [' + @Constraint +']' )
    IF(@@ERROR <> 0)
        GOTO ERR_UPDATING
    FETCH NEXT FROM cur INTO @Name, @Constraint
END

GOTO DONE

ERR_UPDATING:
    RAISERROR('Error dropping constraints', 11, 1)

DONE:

CLOSE cur
DEALLOCATE cur
DROP TABLE #tmp

然后运行SELECT ID FROM tblBrowser

它返回1575296行。什么可能导致这个?我没有任何触发器设置删除索引删除。我正在使用MS SQL Server 2008

编辑:我尝试使用SQL事件探查器来查看是否正在执行任何操作来删除行。没有。

0 个答案:

没有答案