0

我为每个表插入、更新、删除触发器以记录操作。我正在从删除、插入并将这些数据包装到 xml 之前和之后检索数据。但是有些日志不能显示之前和更新值。我的sql语句是:

USE [cop]

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Delete]   ON  [dbo].[Seanslar]   
    AFTER DELETE 
AS 
BEGIN 

SET NOCOUNT ON 

DECLARE @deleted AS XML 
SET @deleted = (select * from deleted for xml AUTO, ELEMENTS XSINIL)

DECLARE @logIslem TINYINT 
SET @logIslem = 3 

DECLARE @tableName VARCHAR(200) 
SELECT @tableName = OBJECT_SCHEMA_NAME( parent_id ) + '.' + OBJECT_NAME( parent_id ) 
  FROM sys.triggers 
 WHERE object_id = @@PROCID  

DECLARE @xmlToChar NVARCHAR(MAX)
SET @xmlToChar = CAST(@deleted AS nvarchar(MAX))

IF LEN(@xmlToChar)<10 
BEGIN 
    IF EXISTS(select * from deleted)
        select @xmlToChar = CAST(seans_id AS NVARCHAR(MAX)) from deleted
    ELSE
        SET @xmlToChar = 'Deleted is empty!'
END

DECLARE @allXml AS XML  
SET @allXml = '<'+@tableName+'>'+ @xmlToChar +'</'+@tableName+'>'  

INSERT INTO [dbo].[Logla]
    ([logIslem], [trgKullanici_id], [tabloAdi], [logXml])     
VALUES           
    (@logIslem, SUSER_NAME(), @tableName, @allXml)

  END

有什么方法可以学习在触发器中执行的“sql语句”?

4

1 回答 1

0

没有实用的方法来捕获由该语句触发的DML触发器内正在执行的 SQL 语句文本。

您可以使用DDL(元数据)触发器执行此操作,但不能使用DML(正常)触发器。


是的,有一种或两种非常不切实际的方法可以做到这一点,但我真的不推荐它们,除非:

  1. 你非常非常精通 SQL,并且
  2. 你真的,真的需要得到它,并且
  3. 您可以负担大量的开发和测试时间
于 2012-11-26T16:22:49.180 回答