SELECT INTO more than one variable

Viewed 81

I am trying to create stored procedure, get some data from database, and then put that data into variables.

Right now i am using this:

DECLARE @CurrentRbr int;
        SET @CurrentRbr = 
        (
           SELECT Max(Rbr) + 1 As CurrentRbr 
           FROM Orders 
           WHERE OrderID=@OrderID 
        ) 

I would like to have few more variables:

DECLARE @OrderAdress varchar(50), @OrderCity varchar (20)

Can I do something like, (this of course does not work):

SET @CurrentRbr,@OrderAdress,@OrderCity = 
        (
           SELECT Max(Rbr) + 1 As CurrentRbr,OrderAdress,OrderCity 
           FROM Orders 
           WHERE OrderID=@OrderID 
        ) 
1 Answers

Select is a better than Set for put that data into variables.

DECLARE @CurrentRbr int,@OrderAdress varchar(50), @OrderCity varchar (20)

       SELECT @CurrentRbr = Max(Rbr) + 1 ,
              @OrderAdress=OrderAdress,
              @OrderCity=OrderCity 
       FROM Orders 
       WHERE OrderID=@OrderID 
       Group by OrderAdress, OrderCity

if you use one variable you don't need SET like below:

DECLARE @CurrentRbr int = (
           SELECT Max(Rbr) + 1 As CurrentRbr 
           FROM Orders 
           WHERE OrderID=@OrderID 
        ) 

You can read another form of use variable in SQL click here I hope you for the best

Related