如何将表1中的几个列复制到表2中,同时定义表2中的某些静态行?

时间:2020-12-08 01:54:54

I understand how to copy a column from one table into another, but I need to do that + also define the values of extra columns in table2 according to local variables within my stored procedure.

我知道如何将一个列从一个表复制到另一个表,但是我需要这样做,并且根据我存储过程中的本地变量定义表2中额外列的值。

Is there a way to do this in one go? Thanks so much for the help!

有办法一次搞定吗?非常感谢你的帮助!

1 个解决方案

#1


4  

If I understand your question correctly, you can simply select the variables' values inline with the columns from the second table, like so:

如果我正确理解了你的问题,你可以简单地选择变量的值与第二表中的列一致,如下所示:

-- Pseudo-declarations just to clarify example
CREATE TABLE t1
(
  col1 INT,
  col2 INT,
  col3 INT,
  colA VARCHAR,
  colB VARCHAR
)

CREATE TABLE t2
(
  col1 INT,
  col2 INT,
  col3 INT
)

DECLARE @varA VARCHAR
DECLARE @varB VARCHAR

[...]

-- You can select whatever other values you need alongside columns from the source table
INSERT INTO t1 (col1, col2, col3, colA, colB)
SELECT col1, col2, col3, @varA, @varB
FROM t2

#1


4  

If I understand your question correctly, you can simply select the variables' values inline with the columns from the second table, like so:

如果我正确理解了你的问题,你可以简单地选择变量的值与第二表中的列一致,如下所示:

-- Pseudo-declarations just to clarify example
CREATE TABLE t1
(
  col1 INT,
  col2 INT,
  col3 INT,
  colA VARCHAR,
  colB VARCHAR
)

CREATE TABLE t2
(
  col1 INT,
  col2 INT,
  col3 INT
)

DECLARE @varA VARCHAR
DECLARE @varB VARCHAR

[...]

-- You can select whatever other values you need alongside columns from the source table
INSERT INTO t1 (col1, col2, col3, colA, colB)
SELECT col1, col2, col3, @varA, @varB
FROM t2