觸發器 trigger 日期 流水 編號

2021-04-13 07:34:12 字數 893 閱讀 8859

create table tb(colid int identity,status int,id nvarchar(14))

gocreate trigger test on tb

for insert,update

asbegin

if update(status)

begin

declare @status int,@colid int

select @status = status,@colid = colid from inserted

if @status = 1

begin

declare @nowdate nvarchar(8),@otherdate nvarchar(8),@num nchar(13),@id int

select @nowdate = convert(char(8),getdate(),112),@otherdate = '19990101',@id = 0,@num = ''

select @otherdate = substring(id,1,8),@id = right(id,4) from tb where substring(id,1,8) = @nowdate

if @otherdate <> @nowdate

begin

select @id = 0

end;

select @num = @nowdate + right((10000 + @id + 1),4)

update tb set id = @num where colid = @colid

end;

end;

end;

goinsert into tb(status) select 1

select * from tb

mysql之觸發器trigger

觸發器 trigger 監視某種情況,並觸發某種操作。觸發器建立語法四要素 1.監視地點 table 2.監視事件 insert update delete 3.觸發時間 after before 4.觸發事件 insert update delete 語法 create trigger trigg...

mysql之觸發器trigger

觸發器 trigger 監視某種情況,並觸發某種操作。觸發器建立語法四要素 1.監視地點 table 2.監視事件 insert update delete 3.觸發時間 after before 4.觸發事件 insert update delete 語法 create trigger trigg...

mysql之觸發器trigger

觸發器 trigger 監視某種情況,並觸發某種操作。觸發器建立語法四要素 1.監視地點 table 2.監視事件 insert update delete 3.觸發時間 after before 4.觸發事件 insert update delete 語法 create trigger trigg...