SQL流水號生成語句

2021-05-11 05:52:57 字數 3838 閱讀 9474

table

num        createdate

---------------------------------

(空)          20090901

(空)          20090901

(空)          20090902

(空)          20090902

(空)          20090903

(空)          20090903

---------------------------------

現在要通過createdate來產生乙個流水號(4位) 按當天來生成 換天要重新生成

即得到結果如下:

table

num        createdate

---------------------------------

200909010001    20090901

200909010002    20090901           

200909020001    20090902

200909020002    20090902

200909030001    20090903

200909030002    20090903

---------------------------------

create

table

tb(id

int, num

varchar(12

),createdate

varchar(8

))insert

into

tb values(1

,'','

20090901')

insert

into

tb values(2

,'','

20090901')

insert

into

tb values(3

,'','

20090902')

insert

into

tb values(4

,'','

20090902')

insert

into

tb values(5

,'','

20090903')

insert

into

tb values(6

,'','

20090903')

goupdate

tb set

num

=createdate

+right('

000'

+cast

((select

count(1

) from

tb where

createdate

=t.createdate

andid

<

t.id) +1

asvarchar),4

) from

tb t

select

*from

tbdrop

table

tb/*

id          num          createdate

----------- ------------ ----------

1           200909010001 20090901

2           200909010002 20090901

3           200909020001 20090902

4           200909020002 20090902

5           200909030001 20090903

6           200909030002 20090903

(所影響的行數為 6 行)*/

declare

@tb1

table([

createdate

]datetime

)insert

@tb1

select

'20090901

'union

allselect

'20090901

'union

allselect

'20090902

'union

allselect

'20090902

'union

allselect

'20090903

'union

allselect

'20090903'--

sql 2005

select

convert

(nvarchar(20

),[createdate],

112) as[

createdate],

convert

(nvarchar(20

),[createdate],

112)

+right('

0000'+

cast

(row_number()

over

(partition by[

createdate

]orderby[

createdate])

asnvarchar(10

)),4

) as

numfrom

@tb1

--sql2000

select

identity

(int,1

,1) as

id,[

createdate

]into

#tem            

from

@tb1

select

convert

(nvarchar(20

),[createdate],

112) as[

createdate],

convert

(nvarchar(20

),[createdate],

112)

+right('

0000'+

cast

((select

count(1

) from

#tem

wheret.[

createdate]=

[createdate

]and

t.id

>

id)+1as

nvarchar(10

)),4

) as

numfrom

#tem t

drop

table

#tem

--測試結果:

/*createdate           num

-------------------- ------------------------

20090901             200909010001

20090901             200909010002

20090902             200909020001

20090902             200909020002

20090903             200909030001

20090903             200909030002

(6 row(s) affected)

*/

SQL生成流水號

經過了幾次的測試終於成功了 declare year int,month int,day int,temp no varchar 12 needno varchar 4 no varchar 20 number varchar 50 randno varchar 50 nu varchar 10 s...

sql 生成流水號

mysql生成流水號 select lpad ifnull max substring business order code,3,4 1,1 4,0 as waternumber from bh customer info business order code的值為前面兩位英文,後面四位數字,例...

SQL自動生成流水號

select convert char 6 getdate 12 下面的 生成長度為8的編號,編號以bh開頭,其餘6位為流水號。得到新編號的函式 create function f nextbh returns char 8 asbegin 從表裡得到最大值加個1000001就增乙個1 return...