Solved

Select Case error "Incorrect syntax near = "

Posted on 2013-06-22
5
763 Views
Last Modified: 2013-06-28
I can never seem to get case statements right.  Object of game,
If PPS.intStage_Number % 2 is even then leave the Proc_Pkg_ID as it is
If PPS.intStage_Number % 2 is odd then increment the Proc_Pkg_ID by 1

I’m getting the error  Incorrect syntax near ‘=’ on the line PP.Proc_Pkg_ID =
I need a hand with getting the syntax right


Declare @CurrStageNo int
Declare @NextStageNo int
Declare @StageResult int


--Testing-----------------------
Declare @Proj_ID int
Declare @Val varchar(20)
--------------------------------
set @Proj_ID = 2
set @Val = '1400270182RS'


SELECT    
      PI.Proj_Item_ID,
      PP.Proc_Pkg_ID =
            case PPS.intStage_Number
            when PPS.intStage_Number % 2 = 0 then PP.Proc_Pkg_ID + 1
            else PP.Proc_Pkg_ID
            end
      FROM    tbl_Projects_PackagesStage PPS
                  INNER JOIN tbl_Process_Packages PP ON PPS.Proc_Pkg_ID = PP.Proc_Pkg_ID
                  INNER JOIN tbl_Package_Stages PPS ON PPS.ProcPkg_Stage_ID = PPS.ProcPkg_Stage_ID
                  INNER JOIN tbl_Projects_Information PI ON PPS.Proj_Item_ID = PI.Proj_Item_ID
      Where
            PI.Proj_ID = @Proj_ID
            AND PI.sItem_Number = @Val
            AND PPIST.bActive = 1
            Order by PPS.intStage_Number Desc
0
Comment
Question by:Steve7423
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39268453
You're close.

case
    when PPS.intStage_Number % 2 = 0 then PP.Proc_Pkg_ID + 1
    else PP.Proc_Pkg_ID
    end as make_up_a_label_here
0
 

Author Comment

by:Steve7423
ID: 39269532
Nope.  I've done that already.  Still the same error.  It's definately the syntax of the case because if I remove it, the query works fine.
0
 

Assisted Solution

by:Steve7423
Steve7423 earned 0 total points
ID: 39269594
Solved !!

two things:

1) The case statement was incorrect as you pointed out
2) I replaced the calculation on the field in the when to a function

Once the case was changed I got the same error.  It dawned on me that there might be nested processes at work, so I created a function that returned the value for the when and Bob's your uncle !

Solution:

SELECT    
      PI.Proj_Item_ID,
      case
            when dbo.Fcn_Modulo_StageNo(PPS.intStage_Number) = 0 then       PP.Proc_Pkg_ID + 1
            else PP.Proc_Pkg_ID
            end as PKGID


[Fcn_Modulo_StageNo]

set @OddEven = (Select
                 case
            When @StageNo % 2 = 0
            then 0
            Else 1
            end as PKGID)
                        
      --Select @OddEven
      return @OddEven
0
 

Author Closing Comment

by:Steve7423
ID: 39283773
There was additional information that needed to be included in order to complete the solution.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39284238
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question