Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > Re: capture and...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 6 Topic 10998 of 11422
Post > Topic >>

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

by "Plamen Ratchev" <Plamen@[EMAIL PROTECTED] > Apr 24, 2008 at 10:34 PM

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(@[EMAIL PROTECTED]
);');
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
 




 6 Posts in Topic:
capture and save running sql query to database table (for loggin
=?ISO-8859-1?Q?Gear=F3id?  2008-04-24 02:12:29 
Re: capture and save running sql query to database table (for lo
"Plamen Ratchev"  2008-04-24 09:30:54 
Re: capture and save running sql query to database table (for
=?ISO-8859-1?Q?Gear=F3id?  2008-04-24 08:14:56 
Re: capture and save running sql query to database table (for lo
"Plamen Ratchev"  2008-04-24 22:34:49 
Re: capture and save running sql query to database table (for lo
"Plamen Ratchev"  2008-04-25 08:59:27 
Re: capture and save running sql query to database table (for
=?ISO-8859-1?Q?Gear=F3id?  2008-05-15 02:49:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 21:04:38 CDT 2008.