SQL Server - Nested case if

Asked By Mass K
06-Apr-09 12:56 PM
Hi, I am not sure why this query is not working. Does anyone have an idea? Thx

(select  case when dbo.fgetstcode( sy.stid ) = 'CNV' then isnull(isnull(max(rate.rate),0) * 0.01 * 0.1 * isnull(max(sy.facevalue),0) / (isnull((select mv.price from linkedvpm.vpm.dbo.mv mv where convert(varchar,date,101) = @InqDate and mv.syid = openev.syid),1)),'No Yield')
 else
 case when dbo.fgetstcode( sy.stid )= 'CVP' then isnull((if(select convert(decimal(15,12), (udf.value)) from linkedvpm.vpm.dbo.USERDEFINEDCOLUMNVALUE UDF where UDF.rowid = openev.syid and UDCOLID = '10000018') IS NULL begin select max(rate.rate) end else (select convert(decimal(15,12), (udf.value)) from linkedvpm.vpm.dbo.USERDEFINEDCOLUMNVALUE UDF where UDF.rowid = openev.syid and UDCOLID = '10000018') end end  ) * 0.5 / (isnull((select mv.price from linkedvpm.vpm.dbo.mv mv where convert(varchar,date,101) = @InqDate and mv.syid = openev.syid),1)),'No Yield')
 else
 case when dbo.fgetstcode( sy.stid )= 'STO' then  isnull(isnull(max(rate.rate),0) * 0.1 * 0.01 * isnull(max(sy.facevalue),0) / (isnull((select mv.price from linkedvpm.vpm.dbo.mv mv where convert(varchar,date,101) = @InqDate and mv.syid = openev.syid),1)),'No Yield')
 else
 case when dbo.fgetstcode( sy.stid )= 'CVF' then  isnull(isnull(max(rate.rate),0) * 0.1 * 0.01 * isnull(max(sy.facevalue),0) / (isnull((select mv.price from linkedvpm.vpm.dbo.mv mv where convert(varchar,date,101) = @InqDate and mv.syid = openev.syid),1)),'No Yield')
 end
 end end end )  as CurrentYield,

I get the following error:
Msg 156, Level 15, State 1, Procedure sp910, Line 190
Incorrect syntax near the keyword 'if'.
Msg 156, Level 15, State 1, Procedure sp910, Line 190
Incorrect syntax near the keyword 'end'.
Msg 170, Level 15, State 1, Procedure sp910, Line 190
Line 190: Incorrect syntax near ','.
Msg 170, Level 15, State 1, Procedure sp910, Line 192
Line 192: Incorrect syntax near ','.
Msg 170, Level 15, State 1, Procedure sp910, Line 194
Line 194: Incorrect syntax near ','.
Msg 156, Level 15, State 1, Procedure sp910, Line 209
Incorrect syntax near the keyword 'as'.

Nested case if  Nested case if

06-Apr-09 01:03 PM
FYI the from part has been omitted but is the actual query
  Santhosh N replied to Mass K
06-Apr-09 01:13 PM
there could be some paranthesis missing or any syntax erorrs in the entire query...

YOu can check the formatting in the analyzer easily and make sure everything is fine
  Mass K replied to Santhosh N
06-Apr-09 01:19 PM
Hi Santhosh,
I checked and rechecked , I don't see any missing...
  Santhosh N replied to Mass K
06-Apr-09 01:21 PM
Can you post the entire code, as the errors are not specific to the block which you posted earlier as we see no if in that...

if case  if case
06-Apr-09 01:23 PM
The if is in the first case
if else  if else
06-Apr-09 01:23 PM
sorry I meant second
hmmm  hmmm
06-Apr-09 01:32 PM
Oh sorry for not seeing but its how it looks ...
Need to check it out in sql query analyzer to check for that...
  Mass K replied to Santhosh N
06-Apr-09 02:15 PM
I did, don't see any error...
if else  if else
06-Apr-09 02:45 PM
I asked a colleague and apparently you're supposed to use nested cases instead or if inside a case
Create New Account
help
Where is the xp_printf on sql server? SQL Server I want to use xp_printf procedure. Where is it ? (SQL Server 2005) SQL Server Programming Discussions SQL Server 2005 (1) Decimal (1) Proc (1) Sprintf (1) Printf (1) Why
How to extract column information of a table using T-sql SQL Server Hi folks, Please advise how to extract column information of a table using T-sql. thanks. PeterK SQL Server Programming Discussions SQL Server 2005 (1) SQL Server (1) UNION ALL (1) NOT NULL (1) Grouping (1) Decimal (1
SQL Express: Failed Reinstall SQL Server Dear Setup Experts: I am trying to reinstall SQL Express after playing with it some. I uninstalled it, and now, it refuses to reinstall in the face. Any ideas what this log file means and how I can get SQL Express installed? I really do not want to have to reinstall my whole system. * ** ** Start of Log File Overall summary: Final result: SQL Server installation failed. To continue, investigate the reason for the failure, correct the problem, uninstall SQL Server, and then rerun SQL Server Setup. Exit code (Decimal): -2068643839 Exit facility code: 1203
what is 2^3 (ask sql server) SQL Server well, tonight i found some weired behavior of sql server, i think its a bug in POWER function if you know what is 2^3 the power of three) hold on . . . there is is twist. . . SELECT POWER(CAST(2 AS DECIMAL(38, 34)), 3) see the result and try it in the sql server (i tested in sql server 2005) it says 7.9999999999999974000000000000000000 similarly 2^2; SS
Problem with casting value under 64-bit sql server 2005 vs under32-bit sql server 2005 SQL Server Hello - I have a strange problem when casting value under 64-bit sql server 2005 vs under 32-bit sql server 2005. I got two servers. One has 32