将插入表和删除表保存到变量中以便在 .NET 代码中使用(通过 sp_oamethod)[SQL Server 2000]
我正在尝试在 SQL Server 中创建一个 SQL 触发器,它将以某种方式序列化插入和删除表以在 .NET 代码中使用(通过 sp_oamethod)。我希望它足够通用,可以用于任何表。
我的第一次尝试涉及使用“for xml”将其序列化为 XML,并将其传递给 .NET 代码。但是,我无法将 XML 分配给变量,因为 SQL Server 2000 不支持它(2005 还不是我们的选择)。唯一的选择是手动进行序列化(请参阅保存FOR XML AUTO 结果到 SQL 中的变量),这不符合我的通用要求。
--This does not work
declare @OldValue varchar(5000)
select @OldValue = (select * from Deleted for XML auto)
有谁知道使用任何方法一般可以做到这一点的方法吗?我不关心格式,只要我可以将列名称和值放入我的 .NET 代码中即可。
I am trying to create a SQL Trigger in SQL Server that will somehow serialize the Inserted and Deleted tables for use in .NET code (via sp_oamethod). I would like this to be generic enough to use for any table.
My first attempt involved using "for xml" to serialize it into XML, and pass it to .NET code. However, I have been unable to assign the XML to a variable, as it is not supported in SQL Server 2000 (2005 is not yet an option for us). The only option is to do the serialization manually (see saving the FOR XML AUTO results to variable in SQL) and that does not fit my requirements of being generic.
--This does not work
declare @OldValue varchar(5000)
select @OldValue = (select * from Deleted for XML auto)
Does anyone know of a way to do this generically, using any method? I do not care about the format, as long as I can get the column names and values into my .NET code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实了解触发器发生在事务内,并且您尝试的触发器会延长事务,更重要的是,依赖于 SQL Server 外部的进程可用并无错误地完成,对吗?
我建议不要在触发器中执行此操作,特别是 sp_OA* 内容。您可以通过将插入和删除表的结果排队到临时表中并让作业或 Windows 服务处理数据来完成您需要的任务吗?
You do understand that triggers take place within a transaction and that one you are attempting will lengthen the transaction and, even more importantly, rely on processes outside of SQL Server being available and completing without error, right?
I'd recommend against doing this in a trigger, especiall the sp_OA* stuff. Can you accomplish what you need by queuing the results from the inserted and deleted tables in a staging table and having a job or windows service process the data?
您是否可以在没有变量的情况下完成此操作,例如:
使触发器返回 XML 结果集?
或者,在触发器内:
有点巴洛克风格,但如果你的音量足够小,它可能会起作用。您还需要添加错误处理,这只是传达了这个想法。
或者,在将数据复制到永久表后,您可以将一个条目添加到队列中,并分别有一些进程来消耗队列并删除表。
更好的是,编写一些代码来自动生成触发器以及系统表之外的所有 XML 生成代码。即,代码生成代码生成XML。
Can you do it without a variable, eg:
Such that the trigger returns an XML result set?
Alternatively, within the trigger:
Kinda baroque, but if you volume is small enough, it might work. You would also want to add error handling, this just communicates the idea.
Or, after you copied the data to a permanent table, you could add an entry to a queue, and separately have some process that consumed the queue and dropped the tables.
Better yet, write some code to auto-generate your triggers and all the XML generating code off the system tables. ie, code generating code generating XML.