Reputation: 3
I am a SQL novice. My problem is I can't find the infinite loop in the code below. The query just keeps executing, but I do not know why. Can anyone point out the mistake in my code?
Additional details: I'll be running this query for 3500 entries. I know my query will probably run too slowly. So I would be interested in hearing about any faster methods as well.
Thanks in advance.
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
Upvotes: 0
Views: 1049
Reputation: 5160
I agree with Steve that you should set the VARCHAR to some length. However, I think that it is actually because @CharacterSet never changes that you're stuck in the infinite loop.
Every statement like this will set you through an infinite loop:
SET @intDecimalValueYear = @intDecimalValueYear / LEN(@CharacterSet)
SET @intDecimalValueMonth = @intDecimalValueMonth / LEN(@CharacterSet)
SET @intDecimalValueDay = @intDecimalValueDay / LEN(@CharacterSet)
-- etc...
It's like when you divide any number by 2 over and over again. It will never reach 0, but get close.
Upvotes: 1
Reputation: 7184
Here's the problem (ignoring the fact that the code seems completely bizarre):
DECLARE @CharacterSet VARCHAR
This declares @CharacterSet as a VARCHAR(1), so dividing by LEN(@CharacterSet) never makes a value smaller, and your code stays in the first WHILE loop. The assignment to @CharacterSet is truncated to one character.
Upvotes: 3