简体   繁体   English

SQL-查找存储过程中的所有插入

[英]SQL - Find all inserts in a stored procedure

long time listener, first time poster. 长时间听众,第一次海报。 I'm new to SQL and am trying to help another co-worker build some scripts to use with a testing tool. 我是SQL的新手,正在尝试帮助另一位同事构建一些脚本以与测试工具一起使用。 Our goal is to have something that can check a table against a set of criteria after an update to our application's code to make sure that the stored procedures are still generating expected data. 我们的目标是在更新我们的应用程序的代码后,拥有能够根据一组条件检查表的内容,以确保存储过程仍在生成预期的数据。

To that end (have I lost you yet?) I'm trying to find a way to find which tables and which columns in those tables are being updated by a given stored proc. 为此(我失去了您吗?)我正在尝试找到一种方法,以查找给定的存储过程正在更新哪些表和那些表中的哪些列。 I've found a script that can tell me what tables a stored procedure uses, but I can't find a way to also find what columns are used. 我找到了一个脚本,该脚本可以告诉我存储过程使用哪些表,但是找不到找到使用哪些列的方法。

This is what I have for finding the tables: 这是我找到表的条件:

WITH stored_procedures AS (
SELECT
o.name AS proc_name, oo.name AS table_name, d.resultobj AS updated,
ROW_NUMBER() OVER(partition by o.name,oo.name ORDER BY o.name,oo.name) AS row
FROM sysdepends d 
INNER JOIN sysobjects o ON o.id=d.id
INNER JOIN sysobjects oo ON oo.id=d.depid
WHERE o.xtype = 'P' AND o.name = 'stored_procedure_here')
SELECT table_name FROM stored_procedures
WHERE row = 1 AND updated = 1 
ORDER BY proc_name,table_name 

Any ideas? 有任何想法吗? Is this possible? 这可能吗?

Thanks! 谢谢!

You could create something like a "protocol-table" who keeps track of all inserts beeing made. 您可以创建类似“协议表”之类的东西,跟踪所有插入的蜜蜂。 This protocol table would be filled by a Trigger wich will be executed every time you insert a row. 该协议表将由触发器填充,每次插入一行时都会执行。

CREATE TRIGGER MyTrigger ON tableName /*Name of table which will have a protocol*/
FOR INSERT
AS

INSERT INTO protocol-table (protocol_id, tableName_id,nameOfTable, GETDATE()) /*protocol_id could be an auto-generated key*/
SELECT NULL, tableName.idField,'tableName', NULL                        /*tableName_id is the ID-Field of the table you protocol*/
FROM inserted /*inserted is a "logical table". Leave this the way it is */

GO

Each time an INSERT is made to your table the ID of this INSERT and the name of the table who was affected by this INSERT would appear in the protocol. 每当一个INSERT是你的表的制作ID此的INSERT和谁是受此影响的表的名称INSERT将出现在该协议。

The downside of this: You would have to do this for every table you have to make sure everything gets protocolled. 缺点:您必须对每个表都执行此操作,以确保所有协议都已协议化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM