search
Homephp教程PHP开发Database trigger gets the sql statement that triggers the trigger

--Trigger

ALTER         TRIGGER   TR_StandardLog   ON   T_Standard  

  FOR   INSERT,DELETE,UPDATE  

AS  


 --ROWCNT

RE @ROWS INT

SET @ROWS=@@ROWCOUNT


SET NOCOUNT ON


--SQL stores the executed sql statement

CREATE TABLE #T (

EVENTTYPE VARCHAR(20),

PARAMETERS INT,

EVENTINFO VARCHAR(300)

)


--Get sql statement
DECLARE @SPID VARCHAR(20)

SET @SPID=CAST(@@SPID AS VARCHAR)

--OPTYPE

DECLARE @OPTYPE INT

SET @OPTYPE=2                                                                  NOT EXISTS (SELECT 1 FROM INSERTED) SET @OPTYPE=3 --DELETE

IF NOT EXISTS (SELECT 1 FROM DELETED) SET @OPTYPE=1 --INSERT

IF @OPTYPE=1
BEGIN
INSERT INTO T_StandardLog(SqlText,OPTYPE,CreateTime)
SELECT 'INSERT INTO TBWEIGH(WEIGHID,FID,CARDNO,STDNNO,PRONAME,PROMODEL,UNIT_FROM,UNIT_TO,TRANSPORT,DRIVER,'
' +'PACKAGE,GROSS_WEIGHT,TARE_WEIGHT,NET_WEIGHT,DEDUCT,FACT_WEIGHT,PRICE, AMOUNT,UNIT,GROSS_TIME ,'
                                                                                                                                           EATETIME)'
                       +'VALUES(:WEIGHID,'' '+isnull(cast(A.F_ID as nvarchar(18)),'null') +''','''+isnull(A.F_CardNo,'null')+''','''+isnull( A.F_StdNo,'null')+''','''+isnull(A.F_ProName,'null')+''','''+isnull(A.F_ProModel,'null')+''' ,'''+isnull(A.F_unitFrom,'null')+''','''+isnull(A.F_UnitTo,'null')+''','''
      +isnull(A.F_Transport, 'null')+''','''+isnull(A.F_Driver,'null')+''','''+isnull(A.F_Memo,'null')+''','+isnull (cast(A.F_Gross as nvarchar(30)),'null')+','+isnull(cast(A.F_Tare as nvarchar(30)),'null')+','
      +isnull(cast( A.F_Net as nvarchar(30)),'null')+','+isnull(cast(A.F_Deduct as nvarchar(30)),'null')+','+isnull(cast(A.F_Fact as nvarchar(30)),'null')+','
                                 +isnull(cast(A.F_Price as nvarchar(30)),'null')+','+isnull(cast(A.F_Money as nvarchar(30) )),'null')+','''+isnull(A.F_Unit,'null')+''','''+isnull(cast(A.F_GrossTime as nvarchar(30)),'null' )+''','''
            +isnull(cast(A.F_TareTime as nvarchar(30)),'null')+''','''+isnull(cast(A.F_NetTime as nvarchar(30)),'null')+''','''+isnull(cast(A.F_edTime as nvarchar(30)),'null')+''','''
            +isnull(A.F_opManGross,'null')+''','''+isnull(A.F_opManTare,'null')+''','''+isnull(A.F_opMan,'null')+''','''+isnull(cast(A.F_edTime as nvarchar(30)),'null')+''','''+isnull(A.F_ScaleNameGross,'null')+''','''+isnull(A.F_ScaleNameTare,'null')+''','''
            +isnull(A.F_ScaleNameNet,'null')+''',sysdate,:UserID,:Remark,'+isnull(cast(A.F_IsCancel as nvarchar(1)),'null')+',:CREATETIME'
            +');','INSERT',GETDATE()
       FROM INSERTED A
    END
  ELSE IF @OPTYPE=3 OR @OPTYPE=2
    BEGIN
       INSERT #T EXEC('DBCC INPUTBUFFER ('+@SPID+')')
       INSERT INTO T_StandardLog(SqlText,OPTYPE,CreateTime) --把执行的sql语句存储在临时表中
       SELECT T.EVENTINFO,CASE @OPTYPE WHEN 1 THEN 'INSERT' WHEN 2 THEN 'UPDATE' WHEN 3 THEN 'DELETE' ELSE ''END,GETDATE() FROM #T T   --从临时表中提取触发的sql语句
    END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


触发器会把数据临时存储在inserted和deleted表中,inserted 和deleted表是逻辑表,只有在触发器中才存在,触发器结束了,就会自动删除,如果是insert操作,则新添加的数据存储在inserted表中,如果是update操作,其实相当于先删除然后再插入,即在deleted表中记录原来的记录,在inserted表中记录更新后的数据,如果是deleted操作,则删除的数据被记录在deleted表中。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software