Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select Case error "Incorrect syntax near = "

Posted on 2013-06-22
5
Medium Priority
?
832 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 66

Accepted Solution

by:
Jim Horn earned 2000 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 66

Expert Comment

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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

876 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