Question on variable

Mikhail Firsov 1,881 Reputation points
2021-05-12T14:23:18.737+00:00

Hello!

My textbook says there are two ways to assign a value to a variable: the SET and the SELECT:
96014-q1.png

Nevertheless I see the following example (on this page) that illustrates the third way (EXEC @VAR ...) :

DECLARE @return_status INT;    
EXEC @return_status = checkstate '2';    
SELECT 'Return Status' = @return_status;    
GO    

Could you imagine any reason why that type of assigning a value to a variable was not mentioned in a book (something like ~that's not the ordinary way to do it, it can only be used for assigning the values from the RETURN and etc...)?

Thank you in advance,
Michael

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,201 Reputation points
    2021-05-13T08:36:04.19+00:00

    Hi @Mikhail Firsov ,

    Thanks for your update.

    Per my knowledge, if you would like to return the value or result of one procedure, the EXEC/Execute is necessary.

    can I assign the return sp value to a variable using only the 3 methods posted above (WITHOUT using OUTPUT parameters)?

    So the answer is NO.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Viorel 118K Reputation points
    2021-05-13T09:35:27.18+00:00

    Maybe the author considered prematurely to show the method based on EXEC because this statement was not described yet.

    By the way, there is a sixth way. Can you spot it? (Hint: that does not include EXEC statement).

    0 comments No comments

  2. Mikhail Firsov 1,881 Reputation points
    2021-05-13T12:33:47.747+00:00

    Thank you all once again for your help!!!

    ... can't choose the answer that "was helpful" - all of them "are helpful"!!! - the previous forum was better as I could vote for multiple answers!!!

    "By the way, there is a sixth way. Can you spot it? (Hint: that does
    not include EXEC statement)."
    - no more ... What is it???


  3. Mikhail Firsov 1,881 Reputation points
    2021-05-14T13:35:18.543+00:00

    ... "By the way, there is a sixth way. Can you spot it? (Hint: that does not include EXEC statement)." - Viorel-1, would you please explain what's the 6th method is?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.