SQL Select a row and store in a SQL variable

后端 未结 3 1606
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 06:16

So, I\'m writing this Stored Proc and I really suck at SQL.

My Question to you guys is:

Can I select an entire row and store it in a variable?

I k

3条回答
  •  深忆病人
    2021-02-05 06:31

    You can select the fields into multiple variables:

    DECLARE @A int, @B int
    
    SELECT
      @A = Col1,
      @B = Col2
    FROM SomeTable
    WHERE ...
    

    Another, potentially better, approach would be to use a table variable:

    DECLARE @T TABLE (
      A int,
      B int
    )
    INSERT INTO @T ( A, B )
    SELECT
      Col1,
      Col2
    FROM SomeTable
    WHERE ...
    

    You can then select from your table variable like a regular table.

提交回复
热议问题