【发布时间】:2020-09-21 11:20:57
【问题描述】:
我希望在我的 SQL 服务器中实现一个触发器,以便在进行报告编辑时保存它们。
服务器运行报告/听写软件包。基本上,“report_text”草稿由 user1 创建,然后由 user2 编辑,然后由 user2 完成。用户 1 的草稿被保存到一个表中,并在数据库中的每次保存时覆盖,一旦用户 1 完成并且用户 2 进行编辑,数据将被进一步覆盖。因此,一旦 user2 保存编辑,user1 的最终草稿就会丢失。
一旦 user1 完成,我正在尝试将“report_text”保存到新表中。
一个单独的表保存一个带有report_id、access_time 和访问报告的用户的审计日志。每次访问都会将新行插入到审计表中,直到最终确定,因此审计表中的每个 report_id 可以有 2 到 4 行(取决于在最终确定之前访问过该文件的用户数)。当每个 report_id 的审计表中每个 report_id 的行数从 2 变为 3 时,触发器应捕获“report_text”。
USE SERVER
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[prelim_text_trigger]
on [dbo].[report_to_user]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO
dbo.report_compare (report_id)
SELECT report_id
FROM inserted
WHERE (SELECT COUNT(report_id)
FROM report_to_user
GROUP BY report_id
HAVING count(report_id) > 2);
END
我尝试了以下代码但没有成功。 dbo.report_compare 中没有插入任何内容,并且在触发器运行时审计表 dbo.report_to_user 存在问题。我认为触发器失败,事务正在回滚。
我做错了什么?我是否应该做出条件声明,以便在不满足条件的情况下进行交易?这是否准确计算了整个表中存在的实例 report_id 的数量,还是仅从临时表 from inserted 中计算?
接下来,如果触发器失败,我希望交易继续进行。如果我在触发器的开头添加XACT_ABORT OFF 或COMMIT TRANS 会起作用吗?
另外,是否应该将FOR EACH ROW 添加到此触发器中,以防数据库同时保存多个报告?
更新
我已经修改了代码,如下:
ALTER trigger [dbo].[report_compare_trigger]
on [dbo].[report_test_to_user]
after insert
as
set xact_abort off
begin
set nocount on;
merge report_compare as target
using (select report_id from inserted) as source (report_id)
on (target.report_id = source.report_id)
when not matched then insert (report_id) values (report_id);
if (select accession_id from report_compare where report_compare.report_id = (select report_id from inserted)) is null
begin
update report_compare
set accession_id = id from accession_test
where report_compare.report_id = (select report_id from inserted) and accession_test.report_id = (select report_id from inserted);
end
update report_compare
set report_compare.id_count = id_count + 1
where report_compare.report_id = (select report_id from inserted);
if (select date_opened from report_compare where report_compare.report_id = (select report_id from inserted)) is null
begin
update report_compare
set date_opened = cre_time from report_test_section
where report_compare.report_id = (select report_id from inserted) and report_test_section.report_id = (select report_id from inserted);
end
if (select user_id from inserted) <> 'EmergencyDept'
begin
if (select users from report_compare where report_compare.report_id = (select report_id from inserted)) is null
begin
update report_compare
set users = isnull(users, '') + user_id from inserted where report_compare.report_id = (select report_id from inserted)
end
else
begin
update report_compare
set users = isnull(users, '') + ', ' + user_id from inserted where report_compare.report_id = (select report_id from inserted)
end
end
end;
【问题讨论】:
标签: sql sql-server count triggers