TRACE trigger triggers in SQL Server

Source: Internet
Author: User

Usually, the data of the database is the instructions sent by the program to be increased and censored. In the background for routine maintenance, including backup, index reconstruction work, of course, there is also a direct implementation of the UPDATE statement to repair the error data, if this behavior occurs on the customer, and the customer is a two knife, then there may be consequences for the situation. So, apart from the institutional clarity of the above-mentioned behavior, can there be some technical traceability?

We can get to the currently executing statement by using the following statement:

 declare   @Handle  varbinary  (64   select   @Handle  =   sql_handle  from   Master. sysprocesses  where  spid=  @ @SPID  select  *  from  sys.fn_get_sql ( @Handle  );  go  

That means knowing the connection spid, we can get to which statement the SPID is currently executing. This is a principle of naming the process from "Activity monitor".

Back to the topic, if you want to monitor data changes, we have to place our own monitoring in the trigger to record the time and location of the event, and the cause of the incident results.

So is the above sentence valid in the trigger, the answer is no.

Here's a big trick:

CREATE TABLE #inputbuffer (      nvarchar       ),smallint,         nvarchar(4000)    )insert into #inputbuffer  EXEC('DBCC INPUTBUFFER (@ @SPID)')

The key is to get a statement that triggers the current SPID, returning three fields. If triggered by a stored procedure, returns the stored procedure name

After taking the original statement, we can determine whether the data changes are triggered by that statement, and provide a basis for us to further judge the culprit.

Currently, this behavior is hindsight, and will lose some of the database performance.

If we want to effectively prevent this situation from the root, we must divide the rights and responsibilities from the system.

TRACE trigger triggers in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.