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
精彩评论