删除内存优化表的触发器

时间:2016-04-09 22:45:25

标签: in-memory database-trigger sql-server-2016 memory-optimized-tables

我正在尝试为SQL Server 2016 RC 2中的内存优化表编写删除触发器

toggleButton.setStyle("-fx-min-width:25;");

当我运行此查询时,我遇到以下错误。 子查询(嵌套在另一个查询中的查询)仅在具有本机编译模块的SELECT语句中受支持。

2 个答案:

答案 0 :(得分:2)

我今天做了一个简单的技巧来解决这个问题,因为我不能在本机编译的存储过程中使用子查询。

场景是从主子表中删除:

row exist in parent -> capture id;

while (rowcount <> 0)
begin
      delete from child where id = idcaptured;
      delete from master where id = idcaptured;

      row exist in parent -> capture id;
end;

答案 1 :(得分:0)

解决方法是使用表变量并使用WHILE循环遍历行。以下是使用FROM解决方法更新UPDATE的示例:

DROP TABLE IF EXISTS dbo.table1
GO
DROP TYPE IF EXISTS dbo.type1
GO


CREATE TABLE dbo.table1
(Id INT NOT NULL PRIMARY KEY NONCLUSTERED,
c2 INT NOT NULL,
LastUpdated DATETIME2 NOT NULL DEFAULT (SYSDATETIME())) 
WITH (MEMORY_OPTIMIZED=ON)
GO
CREATE TYPE dbo.type1 AS TABLE
(Id INT NOT NULL,
c2 INT NOT NULL,
RowID INT IDENTITY,
  INDEX ix_RowID (RowID DESC)) 
WITH (MEMORY_OPTIMIZED=ON)
GO
CREATE TRIGGER dbo.tr_table1
     ON dbo.table1
     WITH NATIVE_COMPILATION, SCHEMABINDING
     AFTER UPDATE
AS BEGIN ATOMIC WITH
(
TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english'
)
     -- original UPDATE with FROM statement:
     --UPDATE dbo.table1
     --SET LastUpdated = SYSDATETIME()
     --FROM dbo.table1 t JOIN Inserted i ON t.Id=i.Id

     DECLARE @tv dbo.type1
     INSERT @tv (Id, c2) 
     SELECT Id, c2 FROM Inserted


     -- workaround that iterates over the rows in the table variable, simulating a cursor:
     DECLARE @max INT = SCOPE_IDENTITY()

     DECLARE @i INT = 1
     DECLARE @Id INT

     WHILE @i <= @max
     BEGIN
         SELECT @Id = Id
         FROM @tv
         WHERE RowID=@i

         UPDATE dbo.table1
         SET LastUpdated = SYSDATETIME()
         WHERE Id = @Id

         SET @i += 1
     END

END
GO

INSERT dbo.table1 (Id, c2) VALUES (1,2)
INSERT dbo.table1 (Id, c2) VALUES (2,2)
GO
SELECT * FROM dbo.table1
GO
UPDATE dbo.table1
SET c2 = 3
GO
SELECT * FROM dbo.table1
GO