--觸發器
ALTER TRIGGER TR_StandardLog ON T_Standard
--ROWCNT
DECLARE @ROWS INT
SET @ROWS=@@ROWCOUNT
--SQL 儲存執行的sql語句
CREATE TABLE #T (
EVENTINFO VARCHAR(300)
)
--取得sql語句
DECLARE @SPID VARCHAR(20)
SET @SPID=CAST(@@SPID AS VARCHAR)
--OPTYPE
DECLARE @OPTYPE
DECLARE @OPTYPE
DECLARE @OPTYPE --UPDATE
IF NOT EXISTS (SELECT 1 FROM INSERTED) SET @OPTYPE=3 FROM DELETED) SET @OPTYPE=1 INSERT INTO T_StandardLog(SqlText,OPTYPE,CreateTime)
SELECT 'INSERT INTO TBWEIGH(WEIGHID,FID,CARDNO,STDNNO,PRONAME,PROMODEL,UNIT_FROM,UNIT_TO,TRANSPORT,DRIVER,') IGHT,DEDUCT,FACT_WEIGHT,PRICE,AMOUNT,UNIT,GROSS_TIME ,'
+'TARE_TIME,NET_TIME,EDIT_TIME,OPMAN_GROSS,OPMAN_TARE,OPMAN,EDITMAN,SCALEN TRANSFER_USER,REMARK,ISCANCELED,CREATETIME)'
+'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,A.F_Transport,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')+','
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.Fchar_Money as nvarchar(30as nvar. )),'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)),'nvarcharull' ',:CREATETIME'
+');','INSERT',GETDATE()
從插入的A
INSERT #T EXEC('DBCC INPUTBUF費率(' +@SPID+')')
INSERT INTO T_StandardLog(SqlText,OPTYPE,CreateTime) -- 將執行的sql語句放在暫存表中
SELECT T.EVENTIN THEN 'UPDATE ' WHEN 3 THEN 'DELETE' ELSE ''END,GETDATE() FROM #T T --從臨時表中提取觸發的sql語句
END
GO
觸發器抽取資料暫存可以在插入和刪除表中,插入和刪除表是邏輯表,只有在觸發中才存在,觸發結束了,自動刪除,如果是插入操作,則新添加的資料儲存在插入表中,如果是更新操作,其實實際上是先刪除然後再插入,即在刪除表中原來記錄的記錄,在插入表中更新記錄後的數據,如果是刪除操作,則刪除的數據被刪除記錄在已刪除表中。