尝试创建一个获取数据的触发器,然后将其替换为应该插入的列。
ALTER trigger beflogs
ON dbo.tbl_logs
INSTEAD OF INSERT
AS
SET NOCOUNT ON
BEGIN
DECLARE @tc INT
SELECT @tc = COUNT(*)
FROM dbo.tbl_logs;VALUES(col1,col2,col3,col4,col5)
UPDATE dbo.tbl_logs
SET id = @tc+1
FROM dbo.tbl_logs
INNER JOIN inserted i ON i.id = dbo.tbl_logs.id
END
似乎缺少的部分,触发器成功完成,但每当我尝试插入数据时,都没有结果。
答案 0 :(得分:0)
请参考此处的示例演示
CREATE TABLE dbo.data (
id INT
,NAME VARCHAR(10)
,StoreId INT
)
CREATE TRIGGER beflogs ON dbo.data
INSTEAD OF INSERT
AS
SET NOCOUNT ON
BEGIN
DECLARE @tc INT
SELECT @tc = COUNT(*)
FROM dbo.data;
INSERT dbo.data (
ID
,NAME
,StoreId
)
SELECT id = @tc + 1
,I.NAME
,I.StoreId
FROM inserted I
END
INSERT INTO DATA (NAME,StoreId) VALUES ('A',10)
INSERT INTO DATA (NAME,StoreId) VALUES ('B',20)
SELECT *FROM DATA