• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

SQL syntax

I have a few variables declared, what I need is to set the variables to some values from a single select from a temp table.

example
declare
@Action varchar(30)
, @CurrentUserID varchar(40)
,@ID int
,@VersionNumber
 
I would like to do something like this
 SELECT @Action , @CurrentUserID = OpsAction, OpsUser from #tmpGroup where ID= @ID and VersionNumber = @CurrentVersionNo

Is there a way to set multiple values from a single select
I know this works
SELECT @Action = OpsAction from #tmpGroup where ID= @ID and VersionNumber = @CurrentVersionNo

but want to avoid repeating the select
0
countrymeister
Asked:
countrymeister
  • 3
  • 2
1 Solution
 
Sreedhar VengalaCommented:
Try something like:
DECLARE @Var1 int
DECLARE @Var1 varchar(20)

-- This line assigns column values to variables
Select @Var1=Col1, @Var2=Col2, from <TableName> where <your condition>)

Select @Var1, @Var2  -- This line prints the values of your variables.
0
 
Pratima PharandeCommented:
Yes it is possible
you can set multiple values from a single select like below
change this as per your requirment @CurrentUserID = OpsAction

SELECT @Action= OpsAction , @CurrentUserID = OpsAction, OpsUser from #tmpGroup where ID= @ID and VersionNumber = @CurrentVersionNo
0
 
Sreedhar VengalaCommented:
Example:

 
 

Declare @TID int 
Declare @SID int 
Select @TID = TransactionID, @SID = StockpileID from Transactions where TransactionID = 41285 
 
Select @TID, @SID 

Open in new window

0
 
Pratima PharandeCommented:
sorry sree_ven , I havn't refresh the page before posting
0
 
Sreedhar VengalaCommented:
No worries :-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now