Solved

Select Case error "Incorrect syntax near = "

Posted on 2013-06-22
5
770 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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