在SQL Server触发器中记录信息

时间:2018-06-25 13:02:59

标签: sql-server tsql triggers

我有一个名为dsReplicated.matDB的表和一个列fee_earner。当该列更新时,我想记录两条信息:

  • dsReplicated.matDB.mt_code
  • dsReplicated.matDB.fee_earner

fee_earner已更新的行中。

当列更新时,我已经掌握了执行某些操作的基本语法,但是需要上面的帮助才能使它生效。

ALTER TRIGGER [dsReplicated].[tr_mfeModified] 
ON [dsReplicated].[matdb]
AFTER UPDATE
AS 
BEGIN
    IF (UPDATE(fee_earner))
    BEGIN
        print 'Matter fee earner changed to '
    END 
END

1 个答案:

答案 0 :(得分:2)

SQL服务器中的触发器的问题在于,每个SQL语句将它们称为一个-每行不会一次。因此,如果您的 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> </head> <body> <div class="row"> <div class="col-12 col-md-10 col-md-8"> <h4>Choose the right app for the job!</h4> <hr style="margin-top: 10px;" /> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <a href="#" class="list-group-item panel-title" data-toggle="collapse" data-target="#s0" data-parent="#accordion" style="background-color: #e5edf4;"> <img id="AccordIcon" src="https://image.flaticon.com/icons/svg/148/148946.svg" /> <span id="AccordionHeadings">File Storage and Sharing</span><span class="glyphicon glyphicon-menu-down pull-right"></span></a> <div id="s0" class="sublinks collapse"> <!-- sublinks collapse --> <a class="list-group-item small d-flex align-items-center" href="/TrainingResourceCenter/O365Training/Pages/OneDrive.aspx"> <img class="img-fluid" style="height: 32px; width: 45px;" src="https://i.imgur.com/e0a3j2l.png" /> <div class=""> <h5>OneDrive</h5> <p id="AccordionText">Store your files in one place, share them with others, and get them from any device connected to the Internet. </p> </div> </a> <a class="list-group-item small d-flex align-items-center" href="/TrainingResourceCenter/O365Training/Pages/SharePointOnline.aspx"> <img class="img-fluid" style="height: 32px; width: 45px;" src="https://i.imgur.com/uii8cJt.png" /> <div> <h5>SharePoint</h5> <p id="AccordionText">Share and manage content, knowledge, and applications to empower teamwork and quickly find information within your organization.</p> </div> </a> <a class="list-group-item small d-flex align-items-center" href="/TrainingResourceCenter/O365Training/Pages/Teams.aspx"> <img class="img-fluid" style="height: 32px; width: 45px;" src="https://i.imgur.com/fV0AGNi.png" /> <div> <h5>Teams</h5> <p id="AccordionText">Microsoft Teams is a chat-based workspace offered in Office 365. Teams makes collaborating with your team easy. </p> </div> </a> </div> </div> </div> </div> </div> </body> </html>语句更新10行,则您的触发器称为一次,并且触发器中的UPDATEInserted伪表每个包含10行数据

为了查看Deleted是否已更改,我建议使用这种方法代替fee_earner函数:

UPDATE()

ALTER TRIGGER [dsReplicated].[tr_mfeModified] ON [dsReplicated].[matdb] AFTER UPDATE AS BEGIN -- I'm just *speculating* here what you want to do with that information - adapt as needed! INSERT INTO dbo.AuditTable (Id, TriggerTimeStamp, Mt_Code, Old_Fee_Earner, New_Fee_Earner) SELECT i.PrimaryKey, SYSDATETIME(), i.Mt_Code, d.fee_earner, i.fee_earner FROM Inserted i -- use the two pseudo tables to detect if the column "fee_earner" has -- changed with the UPDATE operation INNER JOIN Deleted d ON i.PrimaryKey = d.PrimaryKey AND d.fee_earner <> i.fee_earner END 伪表包含Deleted之前 的值-因此,我将UPDATE用作d.fee_earner的值审核表中的“列”。

Old_Fee_Earner伪表包含Inserted之后的 值,因此这就是为什么我从该UPDATE伪表中取出其他值插入审核表。

请注意,您实际上必须在该表中具有不可更改主键,此触发器才能起作用。无论如何,对于SQL Server中的任何数据表,这都是推荐的最佳实践。