2

I am trying to load multiple variables in the same query like this

declare @currentUserPersonnelNumber int
declare @draftWorkFlowStatusId int 
declare @diWorkFlowStatusId int 
declare @ibWorkFlowStatusId int 
declare @ipWorkFlowStatusId int 

select 
    @draftWorkFlowStatusId = case when WFStep='DR' then WorkFlowId else NULL end,
    @diWorkFlowStatusId = case when WFStep='DI' then WorkFlowId else NULL end,
    @ibWorkFlowStatusId = case when WFStep='IB' then WorkFlowId else NULL end,
    @ipWorkFlowStatusId = case when WFStep='IP' then WorkFlowId else NULL end
 from WorkFlow

But only second variable @diWorkFlowStatusId is getting populated and not all.

What is wrong am I doing?

When I do it this way all the variable gets loaded properly, but I think that is not the right way

declare @draftWorkFlowStatusId int = (SELECT WorkFlowId FROM [WorkFlow] WHERE WFStep = 'DR')
declare @diWorkFlowStatusId int = (SELECT WorkFlowId FROM [WorkFlow] WHERE WFStep = 'DI') 
declare @ibWorkFlowStatusId int = (SELECT WorkFlowId FROM WorkFlow WHERE WFStep = 'IB') 
declare @ipWorkFlowStatusId int = (SELECT WorkFlowId FROM WorkFlow WHERE WFStep = 'IP') 
1
  • 1
    I dont see any issue with your query case query ,except variable output will be overridden by last one. Commented Jun 15, 2016 at 9:44

3 Answers 3

3

You have to use an aggregation function :

declare @currentUserPersonnelNumber int
declare @draftWorkFlowStatusId int 
declare @diWorkFlowStatusId int 
declare @ibWorkFlowStatusId int 
declare @ipWorkFlowStatusId int 

select 
    @draftWorkFlowStatusId = MAX(case when WFStep='DR' then WorkFlowId end),
    @diWorkFlowStatusId = MAX(case when WFStep='DI' then WorkFlowId end),
    @ibWorkFlowStatusId = MAX(case when WFStep='IB' then WorkFlowId end),
    @ipWorkFlowStatusId = MAX(case when WFStep='IP' then WorkFlowId end)
 from WorkFlow

Your select as consturcted , can get only a single variable value at a time, since each of this variables are evaluated each time for each record, therefore - the MAX()

If there are more then 1 record that answer the critiria WFStep = ? , then you should tell us which one of them you want.

Sign up to request clarification or add additional context in comments.

9 Comments

Excellent!! Thanks! The only thing is it is giving this warning in the message Warning: Null value is eliminated by an aggregate or other SET operation.
@PawanNogariya That's OK, I'm not sure why this warning is occuring but not problem with this query, also note that you don't have to use ELSE NULL since its the default ELSE .
Got it! One more thing, is this the efficient way to load variables or you suggest something else better?
I can't think of any better way of doing this @PawanNogariya
Yes, that's what he meant @thegameiswar
|
0

It's setting all your variables for every row retrieved from workflow. If only diWorkflowStatusId is set at the end, it's because your last row has WFStep = 'DI'.

Comments

0

Your query returns multiple records, in which case the variables are populated with the values of the last record selected (since you don't give an order by clause it's unpredictable which record that is. Apparently, it's the record where WFStep='DI').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.