我为每个表插入,更新,删除触发器以记录操作。 我正在检索数据从删除,插入和包装到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语句”在触发器内部执行?
答案 0 :(得分:0)
没有切实可行的方法来捕获由该语句触发的 DML 触发器内的执行SQL语句文本。
您可以使用 DDL (元数据)触发器执行此操作,但不能使用 DML (正常)触发器执行此操作。
是的,有一两个 非常 不切实际的方法,但我真的不推荐它们,除非: