SELECT INTO a table variable in T-SQL

前端 未结 8 1896
你的背包
你的背包 2020-11-30 16:55

Got a complex SELECT query, from which I would like to insert all rows into a table variable, but T-SQL doesn\'t allow it.

Along the same lines, you c

8条回答
  •  醉话见心
    2020-11-30 17:50

    The purpose of SELECT INTO is (per the docs, my emphasis)

    To create a new table from values in another table

    But you already have a target table! So what you want is

    The INSERT statement adds one or more new rows to a table

    You can specify the data values in the following ways:

    ...

    By using a SELECT subquery to specify the data values for one or more rows, such as:

      INSERT INTO MyTable 
     (PriKey, Description)
            SELECT ForeignKey, Description
            FROM SomeView
    

    And in this syntax, it's allowed for MyTable to be a table variable.

提交回复
热议问题