473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

capture and save running sql query to database table (for logging)

Hey,

This may sound odd, but is there anyway to catch the current or just
run query from inside a trigger? Kinda like how profiler displays the
query just as you've run it, along with all the statistical data...
But I'm just looking to capture the query itself and save it in a
logging table.

I just need to save an executing query in certain circumstances (if
detected an attempted sql injection attack) for logging purposes.

On MS SQL Server 2005

Hope someone can help...

Thanks!

Gearóid
Jun 27 '08 #1
5 11133
You may not be able to do that in a trigger. In SQL Server 2005 you can get
the current statement with this query:

SELECT [text]
FROM sys.dm_exec_requests AS R
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS S
WHERE session_id = @@SPID;

However, running this inside a trigger returns the SQL statement to create
the trigger.

The only statement that will actually output the current query is:

DBCC INPUTBUFFER(@@SPID);

But you cannot really store the result set from DBCC to a table.

One way to accomplish what you need is to set up a server side trace.

BTW, if the purpose of this is to prevent SQL injection attacks, you can go
the other way around and fix the code to prevent them rather than audit.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
Thanks for the reply, I'll take a look into your suggestions.

But yeah - the injections are happening from older, badly written
classic ASP pages with lots of dynamic sql, which we're looking to
rewrite and fix up, and will also be migrating to .NET pretty soon
anyway. This was kinda just meant as a last resort catch while we're
fixing the pages.
Jun 27 '08 #3
Actually you can save the output from DBCC INPUTBUFFER to a table. Of
course, since a trigger is invoked only on INSERT/UPDATE/DELETE you cannot
audit SELECT statements (for that you can still use server side trace).

Here is a sample trigger that will save the SQL data modification statements
against a table.

-- SQL log table
CREATE TABLE SQLLog (
language_event NVARCHAR(100),
parameters INT,
event_info NVARCHAR(4000),
event_time DATETIME DEFAULT CURRENT_TIMESTAMP);

-- Sample table to audit actions for
CREATE TABLE Foo (
keycol INT PRIMARY KEY,
datacol CHAR(1));

-- Sample data
INSERT INTO Foo VALUES (1, 'a');
INSERT INTO Foo VALUES (2, 'b');
INSERT INTO Foo VALUES (3, 'c');

GO

-- Audit trigger
CREATE TRIGGER LogMySQL
ON Foo
AFTER INSERT, UPDATE, DELETE
AS
INSERT INTO SQLLog (language_event, parameters, event_info)
EXEC('DBCC INPUTBUFFER(@@SPID);');
GO

-- Performs some logged actions
GO

INSERT INTO Foo VALUES (4, 'd');

GO

DELETE Foo
WHERE keycol = 1;

GO

UPDATE Foo
SET datacol = 'f'
WHERE keycol = 2;

GO

-- Perform non-logged action
-- SELECT cannot be logged
SELECT datacol
FROM Foo
WHERE keycol = 4;

GO

-- Check what we have in the log
SELECT event_info, event_time
FROM SQLLog;

/*

-- Results

event_info event_time
-------------------------------- -----------------------
INSERT INTO Foo VALUES (4, 'd'); 2008-04-24
22:24:31.153
DELETE Foo WHERE keycol = 1; 2008-04-24
22:24:31.170
UPDATE Foo SET datacol = 'f' WHERE keycol = 2; 2008-04-24 22:24:31.170

*/

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #4
It is good to note here that DBCC INPUTBUFFER requires the user executing to
be member of the sysadmin fixed server role. One way to handle this is to
specify user or login with sufficient privileges in EXEC (you can use EXEC
AS LOGIN or USER:

INSERT INTO SQLLog (language_event, parameters, event_info)
EXEC('DBCC INPUTBUFFER(@@SPID);') AS LOGIN = 'admin_login';

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #5
Thanks a million Plamen by the way, for your time and help.
Appreciate it. Forgot to say it before!
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christian.Gruber | last post by:
>Description: It would be nice if general query logging could be switched on and off while the MySQL server is running, and not only at startup time. While debugging an application that uses...
0
by: Moritz Steiner | last post by:
Ok, but this only works for the current session, if I close and restart = the client the settings are reset... -----Urspr=FCngliche Nachricht----- Von: Victoria Reznichenko =20 Gesendet:...
3
by: JeffDotNet | last post by:
I wrote a small data processing application that writes a summary of several hundred files. I use drag and drop on a panel (Panel1) to grab the absolute path to each of these files. Then I begin...
28
by: jverri01 | last post by:
First, I am relatively new to working with variables. Most of my experience has been with interface design. i am using ACCESS ver. 2003, running in Windows XP. Second, I spent an hour searching...
0
by: j101 | last post by:
I am attempting to set up Q Capture on RH Linux (x86_64) using DB2 9 fp2, but there seems to be a general problem loading a specify MQ shared library "/opt/mqm/lib/libmqm_r.so". I have MQ v6...
12
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
3
by: evenlater | last post by:
I have an Access application on a terminal server. Sometimes my users need to export reports to pdf, rtf or xls files and save them to their own client device hard drives. They can do that right...
11
by: fniles | last post by:
One of our application uses VB6 and Access97 database. Another application uses VB.NET 2005. This morning for about 15 seconds when the application tries to read either a query or a table from the...
1
by: kiranbabu | last post by:
<html> <head> <style type="text/css"> h2{color:#A02820} </style> </head> <script language=javascript>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.