34,593
社区成员
发帖
与我相关
我的任务
分享
create table tb (编号 varchar(30))
declare @s int
select @s=isnull(cast(right(max(编号),3) as int),0)+1 from tb where substring(编号,4,6)=convert(char(6),getdate(),112)
insert tb
select 'BH-'+convert(char(6),getdate(),112)+right('000'+ltrim(@s),3)
select * from tb
--drop table tb
/**
编号
------------------------------
BH-200901001
BH-200902001
BH-200902002
(所影响的行数为 3 行)
**/
create table tb(col varchar(12))
go
create procedure my_procedure
as
begin
declare @col as varchar(12)
declare @colmax as varchar(12)
if exists(select 1 from tb where substring(col , 4,6) = datename(yy,getdate()) + datename(mm,getdate()))
begin
set @colmax = (select max(col) from tb where substring(col , 4,6) = datename(yy,getdate()) + datename(mm,getdate()))
set @col = left(@colmax,9) + right('000' + cast(cast(right(@colmax,3) as int) + 1 as varchar),3)
end
else
set @col = 'BH-' + datename(yy,getdate()) + datename(mm,getdate()) + '001'
insert into tb values(@col)
end
go
exec my_procedure
select * from tb
drop table tb
drop procedure my_procedure
/*
col
------------
BH-200902001
(所影响的行数为 1 行)
*/
create table tb(col varchar(12))
insert into tb values('BH-200902001')
go
create procedure my_procedure
as
begin
declare @col as varchar(12)
declare @colmax as varchar(12)
if exists(select 1 from tb where substring(col , 4,6) = datename(yy,getdate()) + datename(mm,getdate()))
begin
set @colmax = (select max(col) from tb where substring(col , 4,6) = datename(yy,getdate()) + datename(mm,getdate()))
set @col = left(@colmax,9) + right('000' + cast(cast(right(@colmax,3) as int) + 1 as varchar),3)
end
else
set @col = 'BH-' + datename(yy,getdate()) + datename(mm,getdate()) + '001'
insert into tb values(@col)
end
go
exec my_procedure
select * from tb
drop table tb
drop procedure my_procedure
/*
col
------------
BH-200902001
BH-200902002
(所影响的行数为 2 行)
*/
假设字段为col
create procedure my_procedure
as
begin
declare @col as varchar(12)
declare @colmax as varchar(12)
if exists(select 1 from tb where substring(col , 4,6) = datename(yy,getdate()) + datename(mm,getdate()))
begin
set @colmax = (select max(col) from tb where substring(col , 4,6) = datename(yy,getdate()) + datename(mm,getdate()))
set @col = left(@colmax,9) + right('000' + cast(cast(right(@colmax,3) as int) + 1 as varchar),3)
end
else
set @col = 'BH-' + datename(yy,getdate()) + datename(mm,getdate()) + '001'
end
declare @s int
set @s=1
select @s=cast(right(max(编号),3) as int) from tb where substring(编号,4,6)=convert(char(6),getdate(),112)
select 'BH-'+convert(char(6),getdate(),112)+right('000'+ltrim(@s),3)
--以下代码生成的编号长度为12,前6位为日期信息,格式为YYMMDD,后6位为流水号。
--创建得到当前日期的视图
CREATE VIEW v_GetDate
AS
SELECT dt=CONVERT(CHAR(6),GETDATE(),12)
GO
--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(12)
AS
BEGIN
DECLARE @dt CHAR(6)
SELECT @dt=dt FROM v_GetDate
RETURN(
SELECT @dt+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6)
FROM tb WITH(XLOCK,PAGLOCK)
WHERE BH like @dt+'%')
END
GO
--在表中应用函数
CREATE TABLE tb(
BH char(12) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)
--插入资料
INSERT tb(col) VALUES(1)
INSERT tb(col) VALUES(2)
INSERT tb(col) VALUES(3)
DELETE tb WHERE col=3
INSERT tb(col) VALUES(4)
INSERT tb(BH,col) VALUES(dbo.f_NextBH(),14)
--显示结果
SELECT * FROM tb
/*--结果
BH col
------------------- -----------
050405000001 1
050405000002 2
050405000003 4
050405000004 14
--*/