Advertisement

07.15.2008 at 08:14PM PDT, ID: 23568412
[x]
Attachment Details

Nested Case Statements in SQL 2005 Function?

Asked by MMarlin7 in SQL Server 2005, MS SQL Server, Database

The sql function in the the snippets window gives "green" for ALL rows that call it in an insert/update/select statement. The insert pulls rows from one table to another and doing a calculation (this function) to fill a column in the destination table that's not in the original table. But in being updated, the second table gets "green" in all rows, even those where the ration of progress/target is not >=85%. So i figure it's setting @MonthProgressPercent to the input of first row considered, namely 95/100, and not adjusting it's value for every call. Or i could be smoking crack....Is a nested case statement possible? Am i going about this all wrong? How can i get the variable set in the first case statement, namely @monthprogresspercent, to change and be considered anew each time going into the second case statement, all within one function?


Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
ALTER FUNCTION [dbo].[fn_GetMonthColorIndicator] (@MonthProgress int, @MonthTarget int)
 
RETURNS SQL_VARIANT
AS
BEGIN
DECLARE @MonthColorIndicator SQL_VARIANT, @MonthProgressPercent SQL_VARIANT
 
SET @MonthProgressPercent = (
CASE
WHEN @MonthProgress = 0 OR @MonthTarget = 0 THEN 0
ELSE (CAST(@MonthProgress AS FLOAT)/CAST(@MonthTarget AS FLOAT)*100)
END 
)
 
SET @MonthColorIndicator = (				
CASE 
WHEN @MonthProgressPercent >='85' THEN 'Green'
WHEN @MonthProgressPercent >='60' THEN 'Yellow'
WHEN @MonthProgressPercent >'0' THEN 'Red'
ELSE 'Green' --if zero, then also green (target is zero)
END
		)
RETURN (SELECT @MonthColorIndicator) --gives same indicator for all rows!!!
--RETURN (SELECT @MonthProgressPercent) --will return correct calculation for each insert/update call to this function.
 
END
[+][-]07.16.2008 at 07:41AM PDT, ID: 22016386

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.16.2008 at 07:58AM PDT, ID: 22016576

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, MS SQL Server, Database
Sign Up Now!
Solution Provided By: ScottPletcher
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.16.2008 at 09:14AM PDT, ID: 22017387

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.16.2008 at 09:23AM PDT, ID: 22017475

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628