开发者

How can I copy several columns from table1 into table2 while ALSO defining certain static rows in table2?

开发者 https://www.devze.com 2023-03-04 19:24 出处:网络
I underst开发者_如何学JAVAand 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

I underst开发者_如何学JAVAand 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.

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号