Oiproks
Oiproks

Reputation: 794

Insert using variables and another table's column

I'm trying to write a "simple" INSERT comand in SQL.

INSERT TABLE_A (COLUMN_1_TABLE_A, COLUMN_2_TABLE_A, COLUMN_3_TABLE_A)
        VALUES (@variable_1, @variable_2, (SELECT * FROM TABLE_B))

Now, TABLE_B has one column and a variable number of rows. How can I loop through all TABLE_B rows using its values for all the inserts?

Upvotes: 0

Views: 27

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

You can use SELECT statement with INSERT INTO . . :

INSERT TABLE_A (COLUMN_1_TABLE_A, COLUMN_2_TABLE_A, COLUMN_3_TABLE_A)
    SELECT @variable_1, @variable_2, b.COL
    FROM TABLE_B b

Upvotes: 4

Related Questions