关于SQL产生流水号问题! 超难
我要产生这样的 流水号 是36进制的
如第一个是 000001
第九个是 000009
第十个是 00000a
第三十五个是 00000z
三十六是 000010
[解决办法]
这个是十进制的,看对你有没有帮助?
Id, FormatId, F1 ,F2
Id序号我设了自动加一,FormatId我想他也象这样 "SL000001 ",
当Insert时就加1,FormatId我想他也能自动加一 "SL000001 ", "SL000002 "...
能用一条sql什么办法实现.最好不要用中间表。有什么好方法?
谢谢!
create table #test
(id int identity,
FormatId as 'SL '+right(10000000+id,6),
F1 varchar(50))
go
insert #test(F1) select '1 '
union all select '2 '
select * from #test
drop table #test
/*
id FormatId F1
----------- -------------- -----
1 SL000001 1
2 SL000002 2
(所影响的行数为 2 行)
*/
[解决办法]
这道题不是很难的,先来做个简易的,直接生成流水号的在我做的基础上修改一点即可实现
首先要做个表taa1(id int,bz char(1))插入以下数据
0, '0 '
1, '1 '
....
10, 'a '
....
35, 'z '
要生成流水号的表ta(id int identity,lsh char(6)) id为自动加一字段,lsh即为要自成的流水号
编写以下函数:
create function getvar(@a int) returns char(6)
as
begin
declare @i int,@zs int,@p char(1),@getvar varchar(6)----@zs存储整除的商,@getvar存储流水号
set @i=6----编号的位数,以楼主为例是6位
set @getvar= ' '
while @i> =2
begin
select @zs=@a/case when @i=6 then 60466176---36的5次方,以下数字类推
when @i=5 then 1679616
when @i=4 then 46656
when @i=3 then 1296
when @i=2 then 36 end,@a=@a%case when @i=6 then 60466176
when @i=5 then 1679616
when @i=4 then 46656
when @i=3 then 1296
when @i=2 then 36 end
select @p=taa1.bz from taa1 where taa1.id=@zs---求得对应的36位字符
select @getvar=@getvar+@p
set @i=@i-1
end
select @p=taa1.bz from taa1 where taa1.id=@a----个位余数对应的字符
select @getvar=@getvar+@p
return @getvar
end
通过以上函数 我们便可实现十进制到36进制的转换
如果要实现自动的编号不用辅助字段则要在函数的开头加上一段36进制逆像转换十进制的代码
[解决办法]
--作用:在表中生成一列36進制的ID流水號
--作者:Paoluo 時間:2007.04
--注意:字符串的長度設定為6位
--http://community.csdn.net/Expert/topic/5427/5427373.xml?temp=.9397852
--創建10進制轉為36進制的函數
Create Function Fun_10To36(@ID Int)
Returns Char(6)
As
Begin
Declare @CharID Char(6)
Select @CharID = (Select Numeric36 From tbl10To36 Where Numeric10 = @ID / 60466176)
+ (Select Numeric36 From tbl10To36 Where Numeric10 = @ID % 60466176 / 1679616)
+ (Select Numeric36 From tbl10To36 Where Numeric10 = @ID % 1679616 / 46656)
+ (Select Numeric36 From tbl10To36 Where Numeric10 = @ID % 46656 / 1296)
+ (Select Numeric36 From tbl10To36 Where Numeric10 = @ID % 1296 / 36)
+ (Select Numeric36 From tbl10To36 Where Numeric10 = @ID % 36)
Return @CharID
End
GO
--創建36進制轉為10進制的函數
Create Function Fun_36To10(@CharID Char(6))
Returns Char(6)
As
Begin
Declare @ID Int
Select @ID = (Select Numeric10 From tbl10To36 Where Numeric36 = Substring(@CharID, 1, 1)) * 60466176
+ (Select Numeric10 From tbl10To36 Where Numeric36 = Substring(@CharID, 2, 1)) * 1679616
+ (Select Numeric10 From tbl10To36 Where Numeric36 = Substring(@CharID, 3, 1)) * 46656
+ (Select Numeric10 From tbl10To36 Where Numeric36 = Substring(@CharID, 4, 1)) * 1296
+ (Select Numeric10 From tbl10To36 Where Numeric36 = Substring(@CharID, 5, 1)) * 36
+ (Select Numeric10 From tbl10To36 Where Numeric36 = Substring(@CharID, 6, 1))
Return @ID
End
GO
--創建得到ID的函數
Create Function Fun_GetID()
Returns Char(6)
As
Begin
Declare @ID Char(6)
Select @ID = IsNull(dbo.Fun_10To36(dbo.Fun_36To10(Max(ID)) + 1) , '000001 ') From TEST
Return @ID
End
GO
--創建10進制、36進制數據對照表
Create Table tbl10To36
(Numeric10Int,
Numeric36Char(1))
--創建測試表,ID的默認值為Fun_GetID函數
Create Table TEST(ID Char(6) Default dbo.Fun_GetID(), Name Varchar(10))
GO
--利用臨時表,往10進制、36進制數據對照表中插入對照數據
Select TOP 36 ID = Identity(Int, 0, 1) Into #T From SysColumns A
Insert tbl10To36
Select ID, Rtrim(ID) From #T Where ID <= 9
Union All
Select ID, Char(ID + 87) From #T Where ID > 9
--Select * From tbl10To36
Drop Table #T
GO
--往TEST中循環插入50條數據做測試
Declare @I Int
Select @I = 1
While @I <= 50
Begin
Print @I
Insert TEST(Name) Select 'A ' + Right(100 + @I, 2)
Select @I = @I + 1
End
--檢查TEST表的數據
Select * From TEST
GO
--刪除測試環境
Drop Table TEST
Drop Table tbl10To36
Drop Function Fun_GetID, Fun_36To10, Fun_10To36
--結果
/*
IDName
000001A01
000002A02
000003A03
000004A04
000005A05
000006A06
000007A07
000008A08
000009A09
00000aA10
00000bA11
00000cA12
00000dA13
00000eA14
00000fA15
00000gA16
00000hA17
00000iA18
00000jA19
00000kA20
00000lA21
00000mA22
00000nA23
00000oA24
00000pA25
00000qA26
00000rA27
00000sA28
00000tA29
00000uA30
00000vA31
00000wA32
00000xA33
00000yA34
00000zA35
000010A36
000011A37
000012A38
000013A39
000014A40
000015A41
000016A42
000017A43
000018A44
000019A45
00001aA46
00001bA47
00001cA48
00001dA49
00001eA50
*/