sql 使用触发器自定义动态自增值

来源:互联网 发布:配煤软件 编辑:程序博客网 时间:2024/06/05 00:09
USE [ERP]GO/****** Object:  Trigger [dbo].[setCostApplicationno]    Script Date: 12/26/2013 20:23:17 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[setCostApplicationno]ON [dbo].[Cost_Application]FOR  INSERT ASdeclare @NO varchar(50),@id varchar(50),@maxid int--select @maxid=Isnull(max(right(applyNo,4)),0) from Cost_Applicationselect @maxid=max(id) from Cost_Application--select @NO='FY'+substring(convert(char(6),getdate(),112),3,4)+RIGHT ('000'+cast((@maxid+1) as varchar(50)),4),@id=id  from INSERTEDselect @NO='FY'+(select((select SUBSTRING(cast(GETDATE() as varchar(50)),9,2))+(select SUBSTRING(cast(GETDATE() as varchar(50)),1,2))))+RIGHT ('0000'+cast((@maxid+1) as varchar(50)),4),@id=id  from INSERTEDupdate Cost_Application set applyNo= @NO where id=@id


 


 

0 0
原创粉丝点击