Solved

Select Case error "Incorrect syntax near = "

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using CTE to insert records into a table 2 33
SQL Statement to Update Email Domain 2 29
Challenging SQL Update 5 41
SSRS  - Dropdown with Null 3 24
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 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