对于日志记录系统,我想拥有以下数据库模式:
CREATE TABLE [dbo].[Categories] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[App] NVARCHAR (30) NULL,
[Source] NVARCHAR (30) NULL,
[LogLevel] NVARCHAR (5) NULL,
[Logger] NVARCHAR (120) NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY NONCLUSTERED ([Id] ASC),
CONSTRAINT [UK_Categories] UNIQUE NONCLUSTERED ([App] ASC, [Source] ASC, [LogLevel] ASC, [Logger] ASC)
);
CREATE TABLE [dbo].[Occurences] (
[PointInTime] BIGINT NOT NULL,
[CategoryId] INT NOT NULL,
[Noise] INT NOT NULL,
CONSTRAINT [PK_Occurences] PRIMARY KEY CLUSTERED ([PointInTime] ASC, [CategoryId] ASC, [Noise] ASC),
CONSTRAINT [FK_Category] FOREIGN KEY ([CategoryId]) REFERENCES [Categories] ([Id])
);
设计目标是允许大量的日志记录数据,因为更昂贵的字符串在单独的表中被考虑在内。
从语义上讲,这两个表形成了一个由该视图定义的逻辑表:
CREATE VIEW [dbo].[HistoricLogEntries]
AS SELECT o.PointInTime, o.Noise, c.App, c.[Source], c.LogLevel, c.Logger
FROM Occurences o
JOIN Categories c ON o.CategoryId = c.Id;
我现在想在视图上定义一个替代插入触发器,这是我的麻烦开始的地方。我有以下尝试:
CREATE TRIGGER InsteadTrigger on [dbo].[HistoricLogEntries]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO Categories
SELECT i.App, i.[Source], i.LogLevel, i.Logger
FROM INSERTED i;
INSERT INTO Occurences
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM INSERTED i
JOIN Categories c ON i.App = c.App AND i.[Source] = c.[Source] AND i.LogLevel = c.LogLevel AND i.Logger = c.Logger;
END
显而易见的第一个问题是第一个插入没有检查元组是否已经存在于数据库中。在单值插入的情况下,我知道如何做到这一点,但在这里我必须考虑多行插入。
我无法解释的另一件事是,如果第一次插入成功,触发器甚至不起作用。我收到了外键违规 - 好像第一个插入没有插入任何内容。
我认为这应该是一个常见的设置,所以也许有人有类似的示例代码?
答案 0 :(得分:8)
对于SQL2008 +,我会使用MERGE语句插入新类别:
MERGE dbo.Categories WITH (HOLDLOCK) AS c
USING INSERTED AS i ON i.App = c.App
AND i.[Source] = c.[Source]
AND i.LogLevel = c.LogLevel
AND i.Logger = c.Logger
WHEN NOT MATCHED BY TARGET THEN
INSERT (App, [Source], LogLevel, Logger)
VALUES (i.App, i.[Source], i.LogLevel, i.Logger);
我使用HOLDLOCK
表提示to prevent race condition:
[...]防止并发会话插入具有相同会话的数据 密钥,必须获取不兼容的锁以确保只有一个会话 可以读取密钥(我的注释:在这种情况下为UK_Categories唯一索引)并且该锁必须保持到事务处理 完成。[...]
并防止FK错误:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Category". The conflict occurred in database "Test", table "dbo.Categories", column 'Id'.
我会在第二个INSERT
内添加每个列的名称:
INSERT INTO Occurences (PointInTime, Noise, CategoryId)
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM INSERTED i
JOIN Categories c ON i.App = c.App
这些FK错误的原因是列的顺序不一致:
1)CREATE TABLE中列的顺序是
[PointInTime] BIGINT NOT NULL,
[CategoryId] INT NOT NULL,
[Noise] INT NOT NULL,
但是
2)在第二个插入内,列的顺序不同(参见CategoryId与Noise):
INSERT INTO Occurences
-- or INSERT INTO Occurences (PointInTime, CategoryId, Noise)
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM ...
这是我的解决方案:
ALTER TRIGGER InsteadTrigger on [dbo].[HistoricLogEntries]
INSTEAD OF INSERT
AS
BEGIN
MERGE dbo.Categories WITH (HOLDLOCK) AS c
USING INSERTED AS i ON i.App = c.App
WHEN NOT MATCHED BY TARGET THEN
INSERT (App)
VALUES (i.App);
INSERT INTO Occurences (PointInTime, Noise, CategoryId)
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM INSERTED i
JOIN Categories c ON i.App = c.App
END
GO