日期:2014-05-17 浏览次数:20533 次
select top 20 'CD'+replace(convert(varchar(10),getdate(),120),'-','')+right('0000'+cast(number as varchar),5) as code
from master..spt_values
where type='P' and number>0
结果:
/*
CD2013083100001
CD2013083100002
CD2013083100003
CD2013083100004
CD2013083100005
CD2013083100006
CD2013083100007
CD2013083100008
CD2013083100009
CD2013083100010
CD2013083100011
CD2013083100012
CD2013083100013
CD2013083100014
CD2013083100015
CD2013083100016
CD2013083100017
CD2013083100018
CD2013083100019
CD2013083100020
*/
select top 20 'CD'+replace(convert(varchar(10),getdate(),120),'-','')+right('000'+cast(number as varchar),4) as code
from master..spt_values
where type='P' and number>0
/*
CD201308310001
CD201308310002
CD201308310003
CD201308310004
CD201308310005
CD201308310006
CD201308310007
CD201308310008
CD201308310009
CD201308310010
CD201308310011
CD201308310012
CD201308310013
CD201308310014
CD201308310015
CD201308310016
CD201308310017
CD201308310018
CD201308310019
CD201308310020
*/
DECLARE @length INT, @count INT
SET @length = 4 --指定序列号长度
SET @count = 1000 --指定要生成的序列号个数
;WITH
cte1 AS (SELECT number = 1 UNION ALL SELECT 1),
cte2 AS (SELECT number = 1 FROM cte1 a CROSS JOIN cte1 b),
cte3 AS (SELECT number = 1 FROM cte2 a CROSS JOIN cte2 b),