sql觸發器指令碼編寫之資料按時間歸檔
涉及表:trmSNHUISUB
欄位:
[WAREHOUSE] ,[PLSN_HU] ,[PLSN_BARCODE] ,[PLSN_QRCODE1] ,[PLSN_QRCODE2] ,[PLSN_QRSTATUS] ,[PLSN_QRDATE] ,[PLSN_QRUSER] ,[DELV_GETFLAG] ,[CREATEUSER] ,[CREATEDATE] ,[MODIFYUSER] ,[MODIFYDATE]
建立表語句:
USE [EDEDB_ShellCode] GO
/****** Object: Table [dbo].[trmSNHUISUB] Script Date: 09/28/2018 19:34:11 ******/ SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
CREATE TABLE [dbo].[trmSNHUISUB1101]( [WAREHOUSE] [nvarchar](50) NOT NULL, [PLSN_HU] [nvarchar](20) NOT NULL, [PLSN_BARCODE] [nvarchar](50) NOT NULL, [PLSN_QRCODE1] [nvarchar](200) NULL, [PLSN_QRCODE2] [nvarchar](200) NOT NULL, [PLSN_QRSTATUS] [int] NULL, [PLSN_QRDATE] [datetime] NULL, [PLSN_QRUSER] [nvarchar](20) NULL, [DELV_GETFLAG] [nvarchar](100) NULL, [CREATEUSER] [nvarchar](20) NULL, [CREATEDATE] [datetime] NULL, [MODIFYUSER] [nvarchar](20) NULL, [MODIFYDATE] [datetime] NULL, CONSTRAINT [PK_trmSNHUISUB] PRIMARY KEY CLUSTERED ( [WAREHOUSE] ASC, [PLSN_HU] ASC, [PLSN_BARCODE] ASC, [PLSN_QRCODE2] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
GO
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
觸發器指令碼語法:
ALTER 調整
trigger 觸發器
declare 宣告變數
cursor 遊標,類似於 for i=0;i<10;i++
FETCH_STATUS:
- 0 FETCH 語句成功
-1 FETCH 語句失敗或此行不在結果集中
-2 被提取的行不存在
SUBSTRING:
由 <str> 中的第 <pos> 位置開始,選出接下去的 <len> 個字元。
begin Transaction 可以理解成新建一個還原點。commit Transaction 提交這個自begin tran開始的修改rollback Transaction 表示還原到上個還原點。
附上觸發器指令碼:
USE [EDEDB_ShellCode] GO /****** Object: Trigger [dbo].[tgr_trmSNHUISUB_DELETE] Script Date: 09/28/2018 17:32:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER trigger [dbo].[tgr_trmSNHUISUB_DELETE] on [dbo].[trmSNHUISUB] INSTEAD OF delete as declare @cursor cursor,@WAREHOUSE nvarchar(40),@PLSN_HU nvarchar(40),@barcode nvarchar(50),@PLSN_QRSTATUS int ,@year nvarchar(2),@mouth int ,@tabName varchar(18); set @cursor =cursor for select WAREHOUSE ,PLSN_HU ,PLSN_BARCODE,PLSN_QRSTATUS from deleted open @cursor
fetch next from @cursor into @WAREHOUSE,@PLSN_HU,@barcode,@PLSN_QRSTATUS
while(@@FETCH_STATUS =0)begin if (Len(@barcode)=18) begin set @year=SUBSTRING(@barcode,8,2) set @mouth =Convert(int,SUBSTRING(@barcode,10,2)) end else if (Len(@barcode)=23) begin set @year=SUBSTRING(@barcode,11,2) set @mouth =Convert(int,SUBSTRING(@barcode,13,2)) end set @tabName ='trmSNHUISUB' set @[email protected][email protected] if( @mouth>0 and @mouth <7) set @[email protected]+'01' else if(@mouth >=7 and @mouth<13) set @[email protected]+'07' else set @tabName=null --print (str(@barcode)+'-|-'+str(@year)+'-|-'+str(@mouth)+'-|-'+str(@tabName)) --print(@tabName) if( @PLSN_QRSTATUS<>0 and @tabName is not null and object_id(@tabName, 'U') is not null) begin begin try begin tran exec ('insert into ' + @tabName +'( [WAREHOUSE] ,[PLSN_HU] ,[PLSN_BARCODE] ,[PLSN_QRCODE1] ,[PLSN_QRCODE2] ,[PLSN_QRSTATUS] ,[PLSN_QRDATE] ,[PLSN_QRUSER] ,[DELV_GETFLAG] ,[CREATEUSER] ,[CREATEDATE] ,[MODIFYUSER])select [WAREHOUSE] ,[PLSN_HU] ,[PLSN_BARCODE] ,[PLSN_QRCODE1] ,[PLSN_QRCODE2] ,[PLSN_QRSTATUS] ,[PLSN_QRDATE] ,[PLSN_QRUSER] ,[DELV_GETFLAG] ,[CREATEUSER] ,[CREATEDATE] ,[MODIFYUSER] from trmSNHUISUB where not EXISTS (select * from '[email protected]+' where WAREHOUSE= '''+ @WAREHOUSE +''' and PLSN_BARCODE= '''+ @barcode +''' and PLSN_HU= '''+ @PLSN_HU +''''+') and WAREHOUSE= '''+ @WAREHOUSE +''' and PLSN_BARCODE= '''+ @barcode +''' and PLSN_HU = '''+ @PLSN_HU +'''') exec ('insert into trmSNHUISUBqr(PLSN_QRCODE2,PLSN_QRCODE1,PLSN_BARCODE,TBLNAME)select PLSN_QRCODE2,PLSN_QRCODE1,PLSN_BARCODE,''' + @tabName +''' from ' + @tabName ) delete trmSNHUISUB where [email protected]_HU and [email protected] and [email protected] commit tran end try begin catch rollback tran end catch end fetch next from @cursor into @WAREHOUSE,@PLSN_HU,@barcode,@PLSN_QRSTATUS end
close @cursor deallocate @cursor
---------20180929mocar