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

本文介绍了一个SQL脚本示例,展示了如何创建带有唯一编号的记录表,并使用一个函数来生成唯一的代码,随后插入几条记录并展示最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值