码迷,mamicode.com
首页 > 其他好文 > 详细

生成流水号

时间:2014-10-22 12:24:17      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   for   sp   div   on   

 

 

 1 create table RefNoSeed(
 2     prefix char(4),
 3     seed int
 4 )
 5 
 6 create table RefNoTable(
 7     insertTime datetime unique,
 8     RefNo varchar(20) unique
 9 )
10 
11 if OBJECT_ID(P_GenerateRefNo,P) is not null
12     drop proc P_GenerateRefNo
13 go
14 create proc P_GenerateRefNo
15     @prefix varchar(20),
16     @timeNow datetime
17 as
18     begin try
19         begin tran
20             set nocount on;
21             declare @newSeed int
22             set @newSeed=(select seed from RefNoSeed where prefix=@prefix)+1
23             if @newSeed is null
24                 begin
25                     insert into RefNoSeed values(@prefix,0)
26                     set @newSeed=1
27                 end
28             insert into RefNoTable (insertTime,RefNo)values(@timeNow,@prefix+cast(YEAR(@timeNow) as CHAR(4))+cast(@newSeed as CHAR(10)))
29             update RefNoSeed set seed=seed+1 where prefix=@prefix
30         commit
31     end try
32     begin catch
33         rollback
34     end catch
35 go
36 
37 declare @timenow datetime
38 set @timenow=GETDATE()
39 exec P_GenerateRefNo AB,@timenow
40 waitfor delay 00:00:00.15
41 set @timenow=GETDATE()
42 exec P_GenerateRefNo AB,@timenow
43 waitfor delay 00:00:00.15
44 set @timenow=GETDATE()
45 exec P_GenerateRefNo AB,@timenow
46 waitfor delay 00:00:00.15
47 set @timenow=GETDATE()
48 exec P_GenerateRefNo AB,@timenow
49 waitfor delay 00:00:00.15
50 set @timenow=GETDATE()
51 exec P_GenerateRefNo BF,@timenow
52 waitfor delay 00:00:00.15
53 set @timenow=GETDATE()
54 exec P_GenerateRefNo BF,@timenow
55 waitfor delay 00:00:00.15
56 set @timenow=GETDATE()
57 exec P_GenerateRefNo PF,@timenow
58 waitfor delay 00:00:00.15
59 set @timenow=GETDATE()
60 exec P_GenerateRefNo PF,@timenow
61 waitfor delay 00:00:00.15
62 set @timenow=GETDATE()
63 exec P_GenerateRefNo PF,@timenow
64 waitfor delay 00:00:00.15
65 set @timenow=GETDATE()
66 exec P_GenerateRefNo RG,@timenow
67 
68 select*from RefNoSeed order by prefix
69 select*from RefNoTable order by REFNO
70 
71 delete RefNoSeed
72 delete RefNoTable
73 
74 drop table RefNoSeed
75 drop table RefNoTable

 

生成流水号

标签:style   blog   color   os   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/Tench/p/4042725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!