0
因此,我发现了大量示例,说明如何将动态SQL的结果分配给变量,但我还没有找到一种以我尝试的方式要......所以要么我以这种错误的方式去做,要么我只是没有找到适当的语法来正确执行这个。将动态SQL生成XML的结果存储到XML变量
基本上,我试图使用存储过程从表中删除行(将其视为对表的审计)之前从表中创建一行的XML记录。我试图通过动态SQL来实现这一点,以便可以从多个其他存储过程调用存储过程,这些存储过程在其相应的表上执行删除操作,但在此之前,它们会传递必要的信息(包含select的@sql变量声明获取记录)到此审计存储过程。
Create Procedure dbo.AuditDelete(
@sql NVARCHAR(200),
@TableName VARCHAR(50),
@UserName NVARCHAR(256)
)
AS
DECLARE @XML XML
BEGIN
--at this point @sql will contain a select statement written by the calling stored procedure
--which is specifically written to return the record that is being deleted, example:
@sql = select * from my.table where tableId = 1 FOR XML AUTO
execute sp_executesql @sql, N'@XML XML OUTPUT', @XML = XML OUTPUT;
SELECT @XML
--I was doing this as a check to ensure that XML has the record
--because the execute command returned the XML record, but @XML is null...
--and I can't figure out why
--code for inserting into the Audit table
所以我敢肯定,它无论是一些语法我失踪或者也许我只是要对这个错误的方式。有什么建议么?
注:这是正在做SSMS的SQL Server 2008 R2上
哦。我现在看到的是我正在研究的另一个问题......我觉得愚蠢的哈哈。你会期望如果我做了这个改变并且运行了一个测试(声明@sql和@xml,然后你写了什么,然后'select @ XML'),我只能看到1个结果是XML记录? – Jfabs
@Jfabs是..... –