日期:2014-05-18  浏览次数:20510 次

关于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