SQL syntax

Posted on 2009-04-28
Last Modified: 2012-05-06
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.

@Action varchar(30)
, @CurrentUserID varchar(40)
,@ID int
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
Question by:countrymeister
    LVL 9

    Accepted Solution

    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.
    LVL 39

    Expert Comment

    by:Pratima Pharande
    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
    LVL 9

    Expert Comment

    by:Sreedhar Vengala


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

    Open in new window

    LVL 39

    Expert Comment

    by:Pratima Pharande
    sorry sree_ven , I havn't refresh the page before posting
    LVL 9

    Expert Comment

    by:Sreedhar Vengala
    No worries :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
    Read about achieving the basic levels of HRIS security in the workplace.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now