0

我是 SQL 新手。我的问题是我在下面的代码中找不到无限循环。查询一直在执行,但我不知道为什么。谁能指出我的代码中的错误?

其他详细信息:我将为 3500 个条目运行此查询。我知道我的查询可能会运行得太慢。因此,我也有兴趣了解任何更快的方法。

提前致谢。

DECLARE @intCounter INT,@strNo VARCHAR(8)
DECLARE @strResult VARCHAR(12),@strResult1 VARCHAR(2),@strResult2 VARCHAR(2),@strResult3 VARCHAR(1),@strResult4 VARCHAR(1),@strResult5 VARCHAR(3),@strResult6 VARCHAR(2)
DECLARE @intDecimalValueYear INT,@intDecimalValueMonth INT,@intDecimalValueDay INT,@intDecimalValueTimer1 INT,@intDecimalValueTimer2 INT,@intDecimalValueTimer3 DECIMAL(7,2),@intCounterTemp INT
DECLARE @intRemainder DECIMAL(7,2),@intDividend bigint,@strBranchCode VARCHAR
DECLARE @intWidth1 INT,@intWidth2 INT, @intWidth3 INT
DECLARE @CharacterSet VARCHAR
DECLARE C1 CURSOR FOR SELECT No FROM Pol 
WHERE No='0900001'
OPEN C1
    SET @CharacterSet='— :;?@[\]^ˆ_`{|}~¡¦¨¯´¸¿˜‘”<=>±×÷¢£¤¥§©¬®°µ¶·†‡•…‰€0¼½¾123456789AªÁÀÂÄÃÅÆBCÇDÐEÉÈÊËFƒGHIÍÌÎÏJKLMNÑOºÓÒÔÖÕØŒPQRSŠßTÞ™UÚÙÛÜVWXYÝŸZ'
    SET @intCounter=0
    SET @strResult2=''
    SET @strResult3=''
    SET @strResult4=''
    SET @strResult5=''
    SET @strResult6=''
    FETCH NEXT FROM C1 INTO @strNo
    WHILE @@FETCH_STATUS=0
        BEGIN   
            SET @intCounter = @intCounter + 1
            SET @intDecimalValueYear=YEAR(GETDATE())
            SET @intDecimalValueMonth=MONTH(GETDATE())
            SET @intDecimalValueDay=DAY(GETDATE())
            SET @intDecimalValueTimer1=(DATEPART(HH,CURRENT_TIMESTAMP) * 3600) + (DATEPART(MI,CURRENT_TIMESTAMP) * 60) + (DATEPART(SS,CURRENT_TIMESTAMP))
            SET @intDecimalValueTimer2=(DATEPART(MS,CURRENT_TIMESTAMP))
            SET @intDecimalValueTimer3=@intDecimalValueTimer1+(@intDecimalValueTimer2/1000)
            SET @intCounterTemp=@intCounter
            SET @strResult1='HQ'

            IF @intCounter > LEN(@CharacterSet)
                BEGIN 
                    SET @intCounter = 1 
                END
            --------------
            WHILE @intDecimalValueYear > 0
                BEGIN
                    SET @intRemainder = @intDecimalValueYear % LEN(@CharacterSet)
                    SET @intDecimalValueYear = @intDecimalValueYear / LEN(@CharacterSet)
                    SET @strResult2 = (RIGHT(LEFT(@CharacterSet, @intRemainder + 1),1)) + @strResult2
                END
            IF LEN(@strResult2)=1
                BEGIN
                    SET @strResult2 = '—'
                END
            ELSE IF LEN(@strResult2)=0
                BEGIN
                    SET @strResult2 = '——' + @strResult2
                END
            --------------
            WHILE @intDecimalValueMonth > 0
                BEGIN
                    SET @intRemainder = @intDecimalValueMonth % LEN(@CharacterSet)
                    SET @intDecimalValueMonth = @intDecimalValueMonth / LEN(@CharacterSet)
                    SET @strResult3 = (RIGHT(LEFT(@CharacterSet, @intRemainder + 1),1)) + @strResult3
                END
            IF LEN(@strResult3)=0
                BEGIN
                    SET @strResult3 = '—'
                END
            --------------
            WHILE @intDecimalValueDay > 0
                BEGIN
                    SET @intRemainder = @intDecimalValueDay % LEN(@CharacterSet)
                    SET @intDecimalValueDay = @intDecimalValueDay / LEN(@CharacterSet)
                    SET @strResult4 = (RIGHT(LEFT(@CharacterSet, @intRemainder + 1),1)) + @strResult4
                END
            IF LEN(@strResult4)=0
                BEGIN
                    SET @strResult4 = '—'
                END
            -------------- 
            WHILE @intDecimalValueTimer3 > 0
                BEGIN
                    SET @intRemainder = @intDecimalValueTimer3 % LEN(@CharacterSet)
                    SET @intDecimalValueTimer3 = @intDecimalValueTimer3 / LEN(@CharacterSet)
                    SET @strResult5 = (RIGHT(LEFT(@CharacterSet, @intRemainder + 1),1)) + @strResult5
                END
            IF LEN(@strResult5)=2
                BEGIN
                    SET @strResult5 = '—' + @strResult5
                END
            IF LEN(@strResult5)=1
                BEGIN
                    SET @strResult5 = '——' + @strResult5
                END
            IF LEN(@strResult5)=0
                BEGIN
                    SET @strResult5 = '———'
                END
            -------------
            WHILE @intCounterTemp > 0
                BEGIN
                    SET @intRemainder = @intCounterTemp % LEN(@CharacterSet)
                    SET @intCounterTemp = @intCounterTemp / LEN(@CharacterSet)
                    SET @strResult6 = (RIGHT(LEFT(@CharacterSet, @intRemainder + 1),1)) + @strResult6
                END
            IF LEN(@strResult6)=1
                BEGIN
                    SET @strResult6 = '—' + @strResult6
                END
            ELSE IF LEN(@strResult6)=0
                BEGIN
                    SET @strResult6 = '——'
                END
            -------------
            --SET @strResult=@strResult1 + @strResult2 + @strResult3 + @strResult4 + @strResult5 + @strResult6

            UPDATE POL
            SET ID=@strResult1 + @strResult2 + @strResult3 + @strResult4 + @strResult5 + @strResult6
            WHERE No=@strNo

            FETCH NEXT FROM C1 INTO @strNo
    END
CLOSE C1
DEALLOCATE C1
4

2 回答 2

3

这是问题所在(忽略代码看起来完全奇怪的事实):

DECLARE @CharacterSet VARCHAR

这将@CharacterSet 声明为 VARCHAR(1),因此除以 LEN(@CharacterSet) 永远不会使值变小,并且您的代码将停留在第一个 WHILE 循环中。@CharacterSet 的赋值被截断为一个字符。

于 2012-04-04T17:43:50.427 回答
1

我同意史蒂夫的观点,您应该将 VARCHAR 设置为一定长度。但是,我认为实际上是因为 @CharacterSet 永远不会改变您陷入无限循环。

像这样的每一条语句都会让你进入一个无限循环:

SET @intDecimalValueYear = @intDecimalValueYear / LEN(@CharacterSet)
SET @intDecimalValueMonth = @intDecimalValueMonth / LEN(@CharacterSet)
SET @intDecimalValueDay = @intDecimalValueDay / LEN(@CharacterSet)
-- etc...

就像你一遍又一遍地将任何数字除以 2 一样。它永远不会达到 0,但会接近。

于 2012-04-04T17:51:06.320 回答