帮忙看看

yuanboailanqiu 2011-08-26 11:35:59
如何实现有一组有规则的编号(如200305310001…200305310999)
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiemo587 2011-08-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]
declare @i int
set @i = 1
while @i < 1000
print convert(varchar(8),getdate(),112)+right(10000+@i,4)
[/Quote]
+1

declare @i int
set @i = 1
while @i < 1000
begin
print convert(varchar(8),getdate(),112)+right(10000+@i,4)
set @i=@i+1
end
快溜 2011-08-26
  • 打赏
  • 举报
回复
select 200305310001+number
from master..spt_values
where type='p' and number<999

/*
---------------------------------------
200305310001
200305310002
200305310003
200305310004
200305310005
200305310006
200305310007
200305310008
..
--小F-- 2011-08-26
  • 打赏
  • 举报
回复
--下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号。
--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(8)
AS
BEGIN
RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
END
GO

--在表中应用函数
CREATE TABLE tb(
BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)

--插入资料
BEGIN TRAN
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)
COMMIT TRAN

--显示结果
SELECT * FROM tb
/*--结果
BH col
---------------- -----------
BH000001 1
BH000002 2
BH000003 4
BH000004 14
--*/
AcHerat 2011-08-26
  • 打赏
  • 举报
回复
declare @i int
set @i = 1
while @i < 1000
print convert(varchar(8),getdate(),112)+right(10000+@i,4)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧