从所有表中删除记录,这里有什么问题?

Deleting records from all tables, what's wrong here?

所以我运行正在使用 AppHarbor 创建的 SqlRestore 实用程序,可以在这里找到:

https://github.com/appharbor/AppHarbor-SqlServerBulkCopy

该实用程序的第一步是使用以下命令从目标数据库中清除数据:

// 
StringBuilder commandBuilder = new StringBuilder();
commandBuilder.Append(
    @"
    -- disable all constraints
    EXEC sp_msforeachtable ""ALTER TABLE ? NOCHECK CONSTRAINT all""

    -- delete data in all tables
    EXEC sp_msforeachtable ""DELETE FROM ?""

    -- enable all constraints
    exec sp_msforeachtable ""ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all""
");

当我 运行 这个(通过程序,也通过 Management Studio 手动)时,删除语句抛出错误 invalid column name schoolid

起初,我不知道是哪个 table 引发了错误,所以我将删除步骤重写到游标,如下所示:

declare tableCursor cursor local forward_only for

    select [name]
    from sys.objects
    where [type] = 'U'

    declare @tableName varchar(50)

    open tableCursor

    fetch next from tableCursor into @tableName

    while (@@FETCH_STATUS = 0) begin
        print 'trying to delete from ' + @tableName
        exec('delete from ' + @tableName)
        print 'deleted from ' + @tableName

        fetch next from tableCursor into @tableName
    end

close tableCursor
deallocate tableCursor

以这种方式执行脚本告诉我它是 "trying to delete from table X",但是当我查看 table X 的 table 定义时,该列不存在(而且从来没有)!

接下来,我决定手动删除 table,然后使用 VS Sql 服务器架构比较重新生成 table 到我的目标数据库,因为它是可能以某种方式腐败。

重新生成后,我重新运行删除脚本,它仍然抛出错误!

这是 table 的定义:

CREATE TABLE [dbo].[TourneyPoolMatchResult] (
    [TourneyPoolMatchResultId]      INT            IDENTITY (1, 1) NOT NULL,
    [TournamentTypeId]              INT            NOT NULL,
    [WinningWrestlerId]             INT            NOT NULL,
    [LosingWrestlerId]              INT            NOT NULL,
    [WinType]                       VARCHAR (5)    NOT NULL,
    [Score]                         VARCHAR (20)   NULL,
    [BonusPoints]                   DECIMAL (2, 1) NOT NULL,
    [AdvancementPoints]             DECIMAL (2, 1) NOT NULL,
    [PlacementPoints]               INT            NOT NULL,
    [LosingWrestlerPlacementPoints] INT            NOT NULL,
    PRIMARY KEY CLUSTERED ([TourneyPoolMatchResultId] ASC)
);


GO
CREATE trigger [dbo].[trg_TourneyPoolMatchResult_Change]
    on [dbo].[TourneyPoolMatchResult]
    after insert, update, delete
    as

    exec [UpdateTeamPoints];
    exec [UpdatePointsForSubmittal];

如您所见,table 定义中没有任何地方与 SchoolId 有关。

这到底是怎么回事?

您使用了 delete 语句并且您有一些删除触发器。也许您的触发函数(例如 UpdateTeamPoints 或 UpdatePointsForSubmittal)引用了名为 schoolid 的列?请搜索您的 sql 代码,是否有任何 schoolid?

如果您的表没有 Foreign Key 约束而不是

DELETE FROM table_name;

使用:

TRUNCATE TABLE table_name;

来自 TRUNCATE:

Removes all rows from a table or specified partitions of a table, without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.

TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. For more information, see CREATE TRIGGER (Transact-SQL).