Is there a way to execute a stored procedure every time a database is queried? I'm running SQL Server 2012

I want to be able to do something like the following:

I have database MyDB containing tables Table1 and Table2. I've deprecated Table1, and now every time someone runs any select statement against Table1 (e.g. SELECT * FROM Table1) I want to call a certain stored procedure that will log a message saying Table1 was accessed, so that I know some code is still accessing an old table and can go and remove that dependency.

Is this possible?

有帮助吗?

解决方案

When I want to do something similar to what you are talking-about, I have used a View. (eg. Rename the table from dbo.Table1 to dbo.Table1_Orig and create a View like dbo.Table1, which is a wrapper around the table and includes a SP call or inline-function or equiv). Views can behave like a table and often seem transparent to a user/app.

Otherwise, if your server is not really busy, you might want to consider using the SQL profiler, with a filter for that specific table, so it doesn't record gigs of queries. It will add some overhead onto a server (5%) but otherwise, it is pretty unobtrusive and easy to turn on/off. Some people are really wary of leaving Profiler on for very long, and some companies forbid pointing Profiler at a production DB. So be really careful if you try it. Keep an eye on it. You probably don't want to just leave it running for a few months. That would be evil.

其他提示

I can think of one way to do what you want, but its pretty nasty, so as an alternative:

I'm assuming however that Table2 is effectively a replacement for Table1?

In which case, would it be possible to simply drop Table1 and replace it with a view of the same name that transforms Table2 into the shape of the original Table1?

That way, you wouldn't need to log a message, since any code you have missed will effectively be accessing the correct table by virtue of the view...

If that is no good for you, then here is my pretty hacky answer. It relies on you enabling xp_cmdshell, and uses that as a hack to get around functions not being allowed side effects:

1) Enable xp_cmdshell:

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO

2) Rename Table1 to something else, say Deprecated_Table1

3) Create a function that returns the data from Table1, and also logs a message to your log:

CREATE FUNCTION GetTable1()
RETURNS @Table1 TABLE
(
    ID int
)
AS
BEGIN
    DECLARE @sql varchar(3000)
    DECLARE @cmd varchar(4000)

    SELECT @sql = 'INSERT INTO MyLog Values(''Oops a daisy'')'
    SELECT @cmd = 'sqlcmd -S ' + @@servername + ' -d ' + db_name() + ' -Q "' + @sql + '"'

    EXEC master..xp_cmdshell @cmd, 'no_output'

    INSERT INTO @Table1
    SELECT ID FROM Deprecated_Table1

    RETURN
END

4) Create a view called Table1 that your legacy code will access, that calls the above function, and thus logs a message:

CREATE VIEW Table1 AS SELECT * FROM GetTable1()

Its deeply horrible, but since I guess this is just a temporary measure while you find all of the code you've missed, maybe it'll help you...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top