所以我正在运行AppHarbor创建的SqlRestore实用程序,可以在这里找到:
https://github.com/appharbor/AppHarbor-SqlServerBulkCopy
该实用程序的第一步是使用以下命令从目标数据库中清除数据:
// http://stackoverflow.com/questions/155246/how-do-you-truncate-all-tables-in-a-database-using-tsql/156813#156813
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
起初,我不知道哪个表丢失了错误,所以我将删除步骤重写为游标,如下所示:
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
以这种方式执行脚本告诉我它是"尝试从表X"中删除,但是当我查看表X的表定义时,该列不存在(并且永远不会)!
接下来,我决定只手动删除表,然后使用VS Sql Server Schema比较将表重新生成到我的目标数据库,因为它可能会以某种方式损坏。
重新生成后,我会重新运行该删除脚本,而它仍然会发生错误!
这里是表格定义:
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];
正如您所看到的那样,该表定义中没有任何关于SchoolId的内容。
世界上到底发生了什么?
答案 0 :(得分:2)
您使用delete语句,并且在删除时有一些触发器。也许你的触发器函数(例如UpdateTeamPoints或UpdatePointsForSubmittal)引用了一个名为schoolid的列?请搜索你的sql代码,是否有任何schoolid?
答案 1 :(得分:1)
如果您的表格没有Foreign Key
约束而不是
DELETE FROM table_name;
使用:
TRUNCATE TABLE table_name;
来自TRUNCATE:
从表或表的指定分区中删除所有行, 没有记录单个行删除。 TRUNCATE TABLE是 类似于没有WHERE子句的DELETE语句;然而, TRUNCATE TABLE速度更快,使用的系统和事务日志更少 资源强>
和
TRUNCATE TABLE无法激活触发器,因为操作有效 不记录单个行删除。有关更多信息,请参阅CREATE TRIGGER(Transact-SQL)。