--编号处理-按日期生成单据编号 --创建得到当前日期的视图,以便在用户定义函中可以获取当前日期 create view dbo.v_getdate as select dt=convert(char(6),getdate(),12) --年-月-日 yy-mm-dd go--得到新编号的编号 create function dbo.f_nextbh() returns nvarchar(20) as begin declare @dt char(6) select @dt=dt from dbo.v_getdate
return(select @dt+right(10000001+isnull(right(max(bh),6),0),6) from tb with(xlock,paglock) where bh like @dt+'%') end go --在表中应用函数 create table tb(bh nvarchar(20) primary key default dbo.f_nextbh(),col int) insert tb(col) values(1)insert tb(col) values(3)delete tb where col=3insert tb(bh,col) values(dbo.f_nextbh(),14) select * from tb/*结果 090716000001 1 090716000002 14 */
--创建得到当前日期的视图,以便在用户定义函中可以获取当前日期
create view dbo.v_getdate
as
select dt=convert(char(6),getdate(),12) --年-月-日 yy-mm-dd
go--得到新编号的编号
create function dbo.f_nextbh()
returns nvarchar(20)
as
begin
declare @dt char(6)
select @dt=dt from dbo.v_getdate
return(select @dt+right(10000001+isnull(right(max(bh),6),0),6)
from tb with(xlock,paglock)
where bh like @dt+'%')
end
go
--在表中应用函数
create table tb(bh nvarchar(20) primary key default dbo.f_nextbh(),col int)
insert tb(col) values(1)insert tb(col) values(3)delete tb where col=3insert tb(bh,col) values(dbo.f_nextbh(),14)
select * from tb/*结果
090716000001
1 090716000002
14
*/