Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Policy Based Management in SQL Server 2008
Inside SQL Server Cluster Setup and Troubleshooting Techniques - Part I ...
Configure and Manage Policy Based Management in SQL Server 2008 ...
Using Column Sets with Sparse Columns

More     
 
Latest FAQ's

Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running
How to Display Server Name or IP Address in a Reporting ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> general dba >> Generate Log Files with a Trigger ...

Generate Log Files with a Trigger

By : Ken Kaufman
Oct 12, 2005

Page 2 / 2

Issue resolved, right? Well, not exactly. What happens in the event of having multiple inserts? In this case you need to handle each record separately. In order to accomplish this we need to call upon the dark side, with an evil cursor. Before implementing this, I must give forewarning. Know your application, if records are inserted, updated, or deleted in large chunks you might want to be careful, as this could consume a large amount of memory.

As you can see from the following, this takes our previous example and adds some tweaks by injecting a cursor to loop through all the records in the inserted table. For each record that is inserted, a line is written to the text file, and an event is generated.

ALTER trigger TestTrigger on
tablefortrigger
for insert
as
Declare @Msg varchar(1000)
Declare @CmdString varchar (1000)
Declare GetinsertedCursor cursor for
Select 'Inserted | tablefortrigger | ' + convert(varchar(20), getdate()) + ' | '
+ convert(varchar(5), track)
+ ', ' + lastname + ', ' + firstname
from inserted

open GetinsertedCursor
Fetch Next from GetinsertedCursor
into @Msg

while @@fetch_status = 0
Begin
raiserror( 50005, 10, 1, @Msg)
Fetch Next from GetinsertedCursor
into @Msg
set @CmdString = 'echo ' + @Msg + ' >> C:\logtest.log'
exec master.dbo.xp_cmdshell @CmdString
End
close Getinsertedcursor
deallocate GetInsertedCursor
Now let's test it by performing a multiple insert.
Insert into tablefortrigger(lastname, firstname)
Select lastname, firstname from employees



Conclusion

Before going ahead and implementing this, some thought needs to be given to performance and security. You will see overhead from writing to the text file, and this might not be viable in a database that has 5000 transactions a minute. Since the xp_cmdshell is operating outside of SQL, errors writing to the file will not rollback the transaction. If an intruder uses a covert channel to change your data it will not be logged in the text file. However, the event log will record the DML change. As a best practice, the number of events should be compared to rows in the log file to find any disparity.

There are many ways to accomplish the goal of logging, and many variations to the above script. I hope you'll be able to take this script and make improvements and suggestions on how to make it more efficient.


<< Prev Page         








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved