生成流水号(20060210-0001)的SQL函数

create table t_sql(id int identity(1,1),code char(13),[name] nvarchar(10))

go

create function f_createcode(@bid int,@d datetime)

returns char(13)

as 

    begin

    declare @code char(13)

    declare @c    int

    select @c=count(*) from t_sql

    set @code=replace(convert(char(10),@d,120),'-','')+'-'+cast(right(10000+@c+@bid,4) as char(4))

    return(@code)

    end   

go

 

insert into t_sql(code,[name]) values(dbo.f_createcode(1,getdate()),'ralph')

insert into t_sql(code,[name]) values(dbo.f_createcode(1,getdate()),'king')

insert into t_sql(code,[name]) values(dbo.f_createcode(1,getdate()),'andy')

insert into t_sql(code,[name]) values(dbo.f_createcode(1,getdate()),'test')

 

select * from t_sql

 

go

drop function f_createcode

drop table t_sql

你可能感兴趣的:(SQL函数)