首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

有一个订货单,单据的编号是001-2006-3-31-001,001-2006-3-31-002,依此类退,这样的存储过程如何写

2012-01-15 
有一个订货单,单据的编号是001-2006-3-31-001,001-2006-3-31-002,依此类退,这样的存储过程怎么写有一个订

有一个订货单,单据的编号是001-2006-3-31-001,001-2006-3-31-002,依此类退,这样的存储过程怎么写
有一个订货单,单据的编号是001-2006-3-31-001,001-2006-3-31-002,依此类退,我想获取我正要写入数据库的订单号,
即如果单据的编号已有001-2006-3-31-004,那么下一个编号就是今天001-2006-4-1-001
这样的存储过程怎么写
001-2006-3-31-001
001-2006-3-31-002
001-2006-3-31-003
……
001-2006-4-1-001
001-2006-4-1-002
001-2006-4-1-003
……
依此类推
我想要高效率的,这样的单据编号怎么写,订货单非常多,

[解决办法]
--try

create table T(no nvarchar(20), num int)

create function dbo.get_no(
@curDate datetime
)
returns nvarchar(20)
as
begin
declare @tmp char(10)
set @tmp=convert(char(10), @curDate, 120)

declare @no nvarchar(20)
select @no=max(no) from T
where substring(no, 5, 10)=@tmp

if @no is null
set @no= '001- '+@tmp+ '-001 '
else
begin
set @no=left(@no, 15)+right( '000 '+cast(cast(right(@no, 3) as int)+1 as varchar) , 3)
end

return @no
end

insert T
select dbo.get_no(getdate()), 11
[解决办法]
也寫了一個,ID可以采用默認值,插入數據的時候可以不用輸入。

不過函數中所用的方法是差不多的。

--建立函數
Create Function F_GetID(@GetDate DateTime)
Returns Char(18)
As
Begin
Declare @ID Char(18), @Date Varchar(10)
Select @Date = Convert(Varchar, @GetDate, 120)
Select @ID = Max(ID) From TEST Where CharIndex(@Date, ID) > 0
If @ID Is Null
Select @ID = '001- ' + @Date + '-001 '
Else
Select @ID = Left(@ID, 15) + Right(1001 + Right(@ID, 3), 3)
Return @ID
End
GO
--建立測試環境
Create Table TEST(ID Char(18) Default dbo.F_GetID(GetDate()), Name Varchar(10))
GO
--測試
Insert TEST(Name) Select 'A '
Union All Select 'B '
Union All Select 'C '

Select * From TEST
--刪除測試環境
Drop Table TEST
Drop Function F_GetID
--結果
/*
IDName
001-2007-03-31-001A
001-2007-03-31-002B
001-2007-03-31-003C
*/
[解决办法]
create table t(bianhao varchar(100),createtime datetime)


alter procedure bianhao
as
begin
declare @re varchar(100)
declare @str varchar(100)
declare @i int
declare @str1 varchar(100)
declare @str2 varchar(100)


set @re= '001- '
set @str=convert(varchar(10),getdate(),120)
set @re=@re+@str+ '-001 '

if (select count(1) from t where bianhao=@re)=0
insert into t(bianhao,createtime) select @re,getdate()
else
begin
select @str1=bianhao from t where createtime=(select max(createtime) from t
where convert(varchar(10),createtime,120)=convert(varchar(10),getdate(),120))
set @i= cast(right(@str1,3) as int)
set @i=@i+1
set @str2= '001- '+convert(varchar(10),getdate(),120)+ '- '+
right(power(10,3),3-len(@i))+rtrim(@i)
insert into t(bianhao,createtime) select @str2,getdate()
end

end


select * from t
order by bianhao

bianhao createtime
------------------------------------------


001-2007-03-21-0012007-03-21 14:10:34.973
001-2007-03-21-0022007-03-21 14:10:35.533
001-2007-03-21-0032007-03-21 14:10:35.973
001-2007-03-21-0042007-03-21 14:10:36.373
001-2007-03-21-0052007-03-21 14:10:36.753
001-2007-03-21-0062007-03-21 14:10:37.047
001-2007-03-30-0012007-03-30 14:10:19.503


001-2007-03-30-0022007-03-30 14:10:19.983
001-2007-03-30-0032007-03-30 14:10:20.353
001-2007-03-30-0042007-03-30 14:10:20.687
001-2007-03-30-0052007-03-30 14:10:20.997
001-2007-03-30-0062007-03-30 14:10:21.317
001-2007-03-30-0072007-03-30 14:10:21.817
001-2007-04-24-0012007-04-24 14:11:33.967
001-2007-04-24-0022007-04-24 14:11:35.157
001-2007-04-24-0032007-04-24 14:11:35.817
001-2007-04-24-0042007-04-24 14:11:36.930

(所影响的行数为 17 行)

[解决办法]
再写出函数:

create table t(bianhao varchar(100),createtime datetime)

create function bianhao(@getdate datetime)
returns varchar(100)
as
begin
declare @re varchar(100)
declare @i int
declare @str1 varchar(100)


set @re= '001- '+convert(varchar(10),@getdate,120)+ '-001 '

if (select count(1) from t where bianhao=@re)=0
goto return_exit
else
begin
select @str1=bianhao from t where createtime=(select max(createtime) from t
where convert(varchar(10),createtime,120)=convert(varchar(10),@getdate,120))
set @i= cast(right(@str1,3) as int)+1

set @re= '001- '+convert(varchar(10),@getdate,120)+ '- '+
right(power(10,3),3-len(@i))+rtrim(@i)

end
return_exit:
return @re
end

[解决办法]
如果001超过999张是不是要把001变成002能。如果这样的话可以这样实现:
首先认同tomhuang(春城)的想法,lz可以多增加一个表,表结构如下:
create table tbIdentity
(
date datetime primary key,
qian int,
hou int
)
go

--新建获取今天编号的存储过程(提取,然后更新)

create proc upUpdateIndentity
(
@datetime datetime,
@num varchar(20) output
)
as
begin
--申明变量为int从Identity表取值
declare @qian int
declare @hou int
--申明变量为varchar用来格式化
declare @qianc varchar(6)
declare @houc varchar(6)

begin tran
select @qian = 0,@hou = 0

select @qian = qian,@hou = hou from tbIdentity where date = @datetime

--如果上面的sql语句没有符合条件的,就新增一条记录
if @qian = 0
begin
insert into tbIdentity values(@datetime,1,1)
select @qian = 1,@hou = 1
end

--格式化字符串
select @qianc = '000 ' + cast(@qian as varchar(3)),@houc = '000 ' + cast(@hou as varchar(3)

select @num = substring(@qianc,(len(@qianc) - 3),3)
+ '- ' + @datetime + '- '
+ substring(@houc,(len(@houc) - 3),3)

--这里用来更新Identity表,若◎hou达到999就特殊处理
if @hou = 999
begin
set @hou = 001
set @qian = @qian + 1
end
else
begin
set @hou = @hou + 1
end

update tbIdentity
set qian = @qian,hou = @hou
where date = @datetime

commit
end

这样我们每次取值的话都得到当前应该要的编号,并且同时更新Identity表,可以实时性。这样的效率应该会比在一大堆记录中找的速度快,而且建议在tbIdentity建立集聚索引,因为每个日期只有一条记录,这样效率会更高

[解决办法]
我刚才的脚本有点错误,我到有装数据库的电脑上给我刚才写的脚本做了测试,参考答案为:
create table tbIdentity
(
date datetime primary key,
qian int,
hou int
)
go

--新建获取今天编号的存储过程(提取,然后更新)


create proc upUpdateIndentity
(
@datetime datetime,
@num varchar(20) output
)
as
begin
--申明变量为int从Identity表取值
declare @qian int
declare @hou int


--申明变量为varchar用来格式化
declare @qianc varchar(6)
declare @houc varchar(6)

begin tran
select @qian = qian,@hou = hou from tbIdentity
where convert(varchar(10),date,120) = convert(varchar(10),@datetime,120)

--如果上面的sql语句没有符合条件的,就新增一条记录
if @qian is null
begin
insert into tbIdentity values(@datetime,1,1)
select @qian = 1,@hou = 1
end

--格式化字符串
select @qianc = '000 ' + cast(@qian as varchar(3)),
@houc = '000 ' + cast(@hou as varchar(3))

select @num = substring(@qianc,(len(@qianc) - 2),3)
+ '- ' + convert(varchar(10),@datetime,120) + '- '
+ substring(@houc,(len(@houc) - 2),3)

--这里用来更新Identity表,若@hou达到999就特殊处理
if @hou = 999
begin
set @hou = 1
set @qian = @qian + 1
end
else
begin
set @hou = @hou + 1
end

update tbIdentity
set qian = @qian,hou = @hou
where convert(varchar(10),date,120) = convert(varchar(10),@datetime,120)

commit
end

--测试脚本

declare @num varchar(20)
declare @date datetime
select @date = getdate()
exec upUpdateIndentity @date,@num output
select @num

运行一次:结果为:001-2007-03-31-001
在运行一次,结果为:001-2007-03-31-002

先运行语句:update tbIdentity set hou = 999
然后在运行一次,得到结果为:001-2007-03-31-999
再运行一次,结果为:002-2007-03-31-001

热点排行
Bad Request.