我在更新MS SQL Server 2016中的时态历史记录表中时出现问题。
从文档中,应该需要的步骤如下:
我尝试创建一个执行此操作的过程,但出现此错误:
Msg 13561,Level 16,State 1,Line 23 无法更新时态历史记录表中的行' db.dbo.FooHistory'。
这是我的SQL:
CREATE TABLE Foo(
id int primary key not null
, title nvarchar(50) not null
, startTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL
, endTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL
, PERIOD FOR SYSTEM_TIME (startTime, endTime) )
ALTER TABLE Foo
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.FooHistory));
GO
CREATE PROCEDURE [dbo].[UpdateFooHistory] AS
BEGIN
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
ALTER TABLE dbo.Foo SET (SYSTEM_VERSIONING = OFF);
UPDATE dbo.FooHistory
SET title = 'Foo';
ALTER TABLE dbo.Foo SET (SYSTEM_VERSIONING = ON (
HISTORY_TABLE = dbo.FooHistory,
DATA_CONSISTENCY_CHECK = ON
));
COMMIT TRANSACTION
RETURN 0
END
GO
似乎SQL Server正在检查表是否是临时的"编译时间"而不是在运行时。这是真的?有办法解决它吗?
答案 0 :(得分:4)
将UPDATE语句更改为此语句,它将允许您创建过程:
EXEC(N'UPDATE dbo.FooHistory SET title = ''Foo''');