1

This is a hypothetical question - the problem listed below is entirely fictional, but I believe if anyone has an answer it could prove useful for future reference.

We have a situation wherein multiple systems all populate the same data table on our SQL Server. One of these systems seems to be populating the table incorrectly, albeit in a consistent pattern (leading me to believe it is only a bug in a single system, not multiple) These are majoritively third-party systems and we do not have access to modify or view their source code, nor alter their functionality. We want to file a bug report with the culprit system's developer, but we don't know which one it is as the systems leave no identifiable trace on the table - those in charge before me, when the database was new and only occasionally used by a single system, believed that a single timestamp field was an adequate audit, and this has never been reconsidered.

Our solution has to be entirely SQL-based. Our thought was to write a trigger on the table, and somehow pull through the source of the query - ie, where it came from - but we don't know how, or even if that's possible.

There are some clear solutions to this - for eg contact all the developers to update their software to populate a new software_ID field, and then use the new information to identify the faulty system later (and save my fictional self similar headaches later) - but I'm particularly interested to know if there's anything that could be done purely in-house on SQL Server (or another clever solution) with the restrictions noted.

4

3 回答 3

3

您可以使用以下功能:

select HOST_NAME(), APP_NAME()

因此,您将知道导致更改的计算机和应用程序..

您可以修改应用程序连接字符串以添加自定义应用程序名称,例如:

„Data Source=SQLServerExpress;Initial Catalog=TestDB;
Integrated Security=True; Application Name=MyProgramm”
于 2013-06-27T10:34:45.750 回答
1

您可以使用一个附加nvarchar字段来创建相关表的副本来保存标识符

然后在表上为插入(可能还有更新)创建一个触发器,并在触发器中将相同的行插入到副本中,并添加一个标识符。例如,标识符可以是连接上的登录名:

insert into tableCopy select SUSER_SNAME(), inserted.* from inserted

或者可能是客户端 IP:

declare @clientIp varchar(255);

SELECT clientIp = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = @@SPID

insert into tableCopy select @clientIp, inserted.* from inserted

或者可能是您可以从可以识别客户端应用程序的连接上下文(由于缺少更精确的术语)中获得的其他内容。

确保插入表格副本在任何情况下都不会导致错误。主键和索引可能应该从副本中删除。

于 2013-06-27T10:23:28.087 回答
1

只是一个想法:创建一个触发器,将EXEC sp_who2可疑值存储在表中时获得的信息保存在专用表中。

也许您可以通过状态 RUNNABLE 过滤 sp_who2 值。

因此,如果多个用户共享相同的登录名,您可以确定执行命令的确切时间并由此开始您的研究......

于 2013-06-27T10:23:55.837 回答