Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 values in case statements change to null in while

Author  Topic 

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2007-10-19 : 09:24:14
I'm debugging a stored procedure of mine. here's the basic gist of the code I'm concerned about:

Declare @Num1 int
Declare @Num2 int
Declare @Num3 int
Declare @Num_var varchar (10)

Declare @String varchar (5) -- Set using query
Declare @Value varchar (10) -- Set using query

While @@FETCH_STATUS = 0
BEGIN

-- Result of query is inserted into Fetch statement. The query
-- itself returns something on the order of 1000x rows; so, let's say
-- the result is 10000 rows for argument sake. Consequently,
-- the value of @value may or may not be different on each pass
-- of the while loop. @String may also change, as well.

Fetch Next from Some_Cursor Into @Value, @Coverage, etc...

SET @Num1 = CASE WHEN @String = 'ABC' then @Value END;
SET @Num2 = CASE WHEN @String = 'DEF' then @Value END;
SET @Num3 = CASE WHEN @String = 'GHI' then @Value END;
SET @Num_var = CASE WHEN @String = 'JKL' then @Value END;

...

END -- repeat loop

OK, the problem is on each iteration of the while loop, a previously stored @Num value will be assigned NULL. So, on the first pass, @Value is 500 and @String is ABC, then @Num1 = 500. On the second iteration of the while loop, the @String may or may not be differnt but let's say it's equal to GHI. Nonetheless, regardless of what value is stored in @String, on this second iteration, @Num1 is assigned NULL. The same will ring true for the other Num values except Num3, which will be assigned the value of @value. Now, obviously, all the @Num values start out as NULL.

But what I am trying to accomplish is I only want one of the @Num values to be set on each pass, not all four. If a Num value on a previous pass is 300, it should stay 300 unless it's case condition is met and it's changed to another number like 400.; none of them shouldn't be assigned NULL or remain NULL at the completion of the while loop. Now, when the while loop is completed and I've iterated through the query table, row-by-row, 3 of the Num values will be set to null and the other one will contain a value and this is obviously not what I want.

I can accomplish this fine in an if statment, one would imagine. But I wanted to use case instead. How can I fix my case statement so that all the num values are properly set and do not reset on each pass of the while loop?

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-10-19 : 09:27:04
Don't use CASE, use IF.

IF @String='ABC' SET @Num=@Value

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2007-10-19 : 09:30:08
I know, but for conceptual purposes, is it impossible to carry out this exact task with the CASE conditional?

Thank you.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-10-19 : 10:48:09
@Num1 = CASE WHEN @String = 'ABC' then @Value else @Num1 END

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2007-10-19 : 11:03:58
Yep, that'll work. Great. I suppose there's a hidden else condition of NULL with the manner in which I setup the case statement then...Thank you!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-10-19 : 12:00:59
yes, read about CASE in Books On Line to be sure you understand how it works. It is not a statement, it is an expression, that simply returns a value.

see: http://weblogs.sqlteam.com/jeffs/archive/2007/05/03/60195.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2007-10-19 : 13:10:16
OK. Thank you again.
Go to Top of Page
   

- Advertisement -