生成流水号(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

  

 

原文地址:http://blog.csdn.net/ldw701/article/details/597688

posted @ 2015-02-11 15:57  程序猿网友666  阅读(833)  评论(0编辑  收藏  举报