Link to home
Start Free TrialLog in
Avatar of Marius0188
Marius0188

asked on

MS SQL 2005 :: SET Variable Value Dynamically Based On Select Field's Value

Dear Experts,

Is it possible to assign different values to a parameter in a SQL statement based on your select field's value.

Please see code snipped for a clearer understanding.
DECLARE @P INT
 
SELECT
	P2.PartNo, P2.GroupingCode, P2.Code, P2.Qty,
 
	CASE WHEN P2.Qty > 10 THEN 
		SET @P = 10000
 
	ELSE 
		SET @P = -99 
	END,
        @P AS VarValue
FROM
	tblPickingList P2

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you cannot SELECT at SET in the same statement.
anyhow, do you really need the @p?
SELECT
      P2.PartNo, P2.GroupingCode, P2.Code, P2.Qty,
 
      CASE WHEN P2.Qty > 10 
           THEN  10000 
           ELSE  -99 
      END AS VarValue
FROM
      tblPickingList P2

Open in new window

Avatar of Marius0188
Marius0188

ASKER

Yes, I just use it in that simple example for explanation purposes.
Where I will be using it it could ease my life a lot. :)
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What do you mean with "just a single value" ?

Thanks for quick replies.
:)
as I wrote: just 1 single value.

you did not really explain what you actually try to achieve, so I can only give "guessings" ...