Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Select Case error "Incorrect syntax near = "

Posted on 2013-06-22
5
Medium Priority
?
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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