在我們的數據庫中,我們使用主鍵作爲數字0到9以及字母表中所有不是誓言的字母的組合。包括字母Y. 我創建了這個表來組織它。在SQL Server中將Base 31的值轉換爲BigInt
CREATE Table Base31
(ID varchar(1),
NumberValue bigint)
GO
Insert into Base31 SELECT '0', '0'
Insert into Base31 SELECT '1', '1'
Insert into Base31 SELECT '2', '2'
Insert into Base31 SELECT '3', '3'
Insert into Base31 SELECT '4', '4'
Insert into Base31 SELECT '5', '5'
Insert into Base31 SELECT '6', '6'
Insert into Base31 SELECT '7', '7'
Insert into Base31 SELECT '8', '8'
Insert into Base31 SELECT '9', '9'
Insert into Base31 SELECT 'B', '10'
Insert into Base31 SELECT 'C', '11'
Insert into Base31 SELECT 'D', '12'
Insert into Base31 SELECT 'F', '13'
Insert into Base31 SELECT 'G', '14'
Insert into Base31 SELECT 'H', '15'
Insert into Base31 SELECT 'J', '16'
Insert into Base31 SELECT 'K', '17'
Insert into Base31 SELECT 'L', '18'
Insert into Base31 SELECT 'M', '19'
Insert into Base31 SELECT 'N', '20'
Insert into Base31 SELECT 'P', '21'
Insert into Base31 SELECT 'Q', '22'
Insert into Base31 SELECT 'R', '23'
Insert into Base31 SELECT 'S', '24'
Insert into Base31 SELECT 'T', '25'
Insert into Base31 SELECT 'V', '26'
Insert into Base31 SELECT 'W', '27'
Insert into Base31 SELECT 'X', '28'
Insert into Base31 SELECT 'Y', '29'
Insert into Base31 SELECT 'Z', '30'
我想將這些主鍵轉換爲數字值,但我一直運行int int限制。我已經改變了所有使用大的int數據類型。我怎樣才能讓算法使用大的int數據類型?
GO
CREATE Proc Base_31 @v varchar(8)
as
DECLARE @Ai bigint
DECLARE @Bi bigint
DECLARE @Ci bigint
DECLARE @Di bigint
DECLARE @Ei bigint
DECLARE @Fi bigint
DECLARE @Gi bigint
DECLARE @Hi bigint
SELECT @Ai = NumberValue from Base31 where RIGHT(@v,1) = ID
SELECT @Bi = (NumberValue * POWER(31,1)) from Base31 where LEFT(RIGHT(@v,2),1) = ID
SELECT @Ci = (NumberValue * POWER(31,2)) from Base31 where LEFT(RIGHT(@v,3),1) = ID
SELECT @Di = (NumberValue * POWER(31,3)) from Base31 where LEFT(RIGHT(@v,4),1) = ID
SELECT @Ei = (NumberValue * POWER(31,4)) from Base31 where LEFT(RIGHT(@v,5),1) = ID
SELECT @Fi = (NumberValue * POWER(31,5)) from Base31 where LEFT(RIGHT(@v,6),1) = ID
SELECT @Gi = (NumberValue * POWER(31,6)) from Base31 where LEFT(RIGHT(@v,7),1) = ID
SELECT @Hi = (NumberValue * POWER(31,7)) from Base31 where LEFT( @v ,1) = ID
SELECT @v AS Original
, Cast(@Ai AS bigint)
+ Cast(@Bi AS bigint)
+ Cast(@Ci AS bigint)
+ Cast(@Di AS bigint)
+ Cast(@Ei AS bigint)
+ Cast(@Fi AS bigint)
+ Cast(@Gi AS bigint)
+ Cast(@Hi AS bigint)
as [Base 31 converted]
GO
但是這個PK定義沒有語義,它只是一個句柄。它可以被任何任意的句柄取代,例如單調遞增的int。 – 2013-03-07 18:11:35