SQL Server 中的触发器能否将数据写入单独服务器上单独数据库中的表?

发布于 2024-12-09 20:17:53 字数 2213 浏览 0 评论 0原文

出于安全目的,我想为我的数据库开发一个审核系统,将审核数据写入单独服务器上的数据库。服务器位于同一域,并且已创建适当的帐户权限。它们都是运行 SQL Server 2008 R2 的 Windows Server 2008 R2 服务器。当审计表与主系统 (DMCS) 位于同一数据库中时,当前触发器可以正常工作。

USE [DMCS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[AUDIT_SAMPLE] on dbo.SAMPLE
FOR INSERT, UPDATE, DELETE
AS
  DECLARE @OLD VARCHAR(MAX);
  DECLARE @NEW VARCHAR(MAX);
  DECLARE @RECORDID INT;
  DECLARE @USERID INT;
  DECLARE @ID INT;
  SET @ID = ((SELECT MAX(Audit_Action_ID) AS MaxID FROM DMCS_AUDIT_ACTION) + 1);
  -- Increments ID column, you could also simply set it as an identity field with auto-increment

  -- IF UPDATE (Action_Type_ID = 3)
  IF EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM deleted) AND EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM inserted)
  BEGIN
    SET @RECORDID = (SELECT Sample_ID FROM deleted);
    SET @USERID = (SELECT Sample_User_ID FROM deleted);
    SET @OLD = (SELECT Sample_Data FROM deleted);
    SET @NEW = (SELECT Sample_Data FROM inserted);
    INSERT INTO DMCS_AUDIT_ACTION VALUES (@ID, 6, 3, @USERID, @RECORDID, USER, @OLD, @NEW, GETDATE(), 'S');
  END

  -- IF DELETE (Action_Type_ID = 4)
  IF EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM deleted) AND NOT EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM inserted)
  BEGIN
    SET @RECORDID = (SELECT Sample_ID FROM deleted);
    SET @USERID = (SELECT Sample_User_ID FROM deleted);
    SET @OLD = (SELECT Sample_Data FROM deleted);
    INSERT INTO DMCS_AUDIT_ACTION VALUES (@ID, 6, 4, @USERID, @RECORDID, USER, @OLD, 'N/A', GETDATE(), 'S');
  END

  -- IF INSERT (Action_Type_ID = 2)
  IF NOT EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID old FROM deleted) AND EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID new FROM inserted)
  BEGIN
    SET @RECORDID = (SELECT Sample_ID FROM inserted);
    SET @USERID = (SELECT Sample_User_ID FROM inserted);
    SET @NEW = (SELECT Sample_Data FROM inserted);
    INSERT INTO DMCS_AUDIT_ACTION VALUES (@ID, 6, 2, @USERID @RECORDID, USER, 'N/A', @NEW, GETDATE(), 'S');
  END

所有 SELECT 语句都引用主服务器上的 DMCS 数据库(这是每个正在审核的表的触发器所在的位置),并且所有 INSERT 命令都需要写入单独服务器上的单独数据库。如何在触发器中执行此操作而不创建存储过程?

I want to develop an auditing system for my database that writes auditing data to a database on a separate server, for security purposes. The servers are on the same domain, and proper account permissions have been created. They are both Windows Server 2008 R2 servers running SQL Server 2008 R2. Here is the current trigger that works fine when the auditing tables are in the same database as the main system (DMCS).

USE [DMCS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[AUDIT_SAMPLE] on dbo.SAMPLE
FOR INSERT, UPDATE, DELETE
AS
  DECLARE @OLD VARCHAR(MAX);
  DECLARE @NEW VARCHAR(MAX);
  DECLARE @RECORDID INT;
  DECLARE @USERID INT;
  DECLARE @ID INT;
  SET @ID = ((SELECT MAX(Audit_Action_ID) AS MaxID FROM DMCS_AUDIT_ACTION) + 1);
  -- Increments ID column, you could also simply set it as an identity field with auto-increment

  -- IF UPDATE (Action_Type_ID = 3)
  IF EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM deleted) AND EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM inserted)
  BEGIN
    SET @RECORDID = (SELECT Sample_ID FROM deleted);
    SET @USERID = (SELECT Sample_User_ID FROM deleted);
    SET @OLD = (SELECT Sample_Data FROM deleted);
    SET @NEW = (SELECT Sample_Data FROM inserted);
    INSERT INTO DMCS_AUDIT_ACTION VALUES (@ID, 6, 3, @USERID, @RECORDID, USER, @OLD, @NEW, GETDATE(), 'S');
  END

  -- IF DELETE (Action_Type_ID = 4)
  IF EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM deleted) AND NOT EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID FROM inserted)
  BEGIN
    SET @RECORDID = (SELECT Sample_ID FROM deleted);
    SET @USERID = (SELECT Sample_User_ID FROM deleted);
    SET @OLD = (SELECT Sample_Data FROM deleted);
    INSERT INTO DMCS_AUDIT_ACTION VALUES (@ID, 6, 4, @USERID, @RECORDID, USER, @OLD, 'N/A', GETDATE(), 'S');
  END

  -- IF INSERT (Action_Type_ID = 2)
  IF NOT EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID old FROM deleted) AND EXISTS (SELECT Sample_ID, Sample_Data, Sample_User_ID new FROM inserted)
  BEGIN
    SET @RECORDID = (SELECT Sample_ID FROM inserted);
    SET @USERID = (SELECT Sample_User_ID FROM inserted);
    SET @NEW = (SELECT Sample_Data FROM inserted);
    INSERT INTO DMCS_AUDIT_ACTION VALUES (@ID, 6, 2, @USERID @RECORDID, USER, 'N/A', @NEW, GETDATE(), 'S');
  END

All of the SELECT statements are referencing the DMCS database on the primary server (which is where the triggers are located for each table being audited), and all the INSERT commands need to write to a separate database on a separate server. How do I do this within the trigger without creating a stored procedure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-12-16 20:17:53

两者皆是。

您将更改为使用 3 或 4 部分对象名称

  • OtherDB.dbo.OtherTable
  • LinkedServer.RemoteDB.dbo.Table

我个人会使用触发器中的服务代理将数据发送到另一台服务器。我不想因为链接服务器而导致写入失败。

或者只是在本地登录并稍后使用 SSIS 任务等发送到中央审计服务器

Yes to both.

You'd change to use 3 or 4 part object names

  • OtherDB.dbo.OtherTable
  • LinkedServer.RemoteDB.dbo.Table

Personally I'd use service broker from the trigger to send data to another server. I don't want to a write to fail because of a linked server.

Or just log locally and send to central audit server later with an SSIS taks or such

幻梦 2024-12-16 20:17:53

如果数据库位于不同的服务器上,您需要创建一个链接服务器然后你可以使用 4 部分表示法

You need to create a linked server if the DB is on a different server and then you can use 4 part notation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文