Maksym Rybalkin
Maksym Rybalkin

Reputation: 543

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 - 9 (for example, there can be more than 20 ids).

I have one varchar value = 'premium'.

I need to insert these values to another table, after this action I should have 9 records with id from the first table and 'premium' varchar in the second table:

1, 'premium';
2, 'premium';
etc.

How to write the function for SQL?

Upvotes: 0

Views: 789

Answers (2)

S-Man
S-Man

Reputation: 23676

Do you want this?

demo:db<>fiddle

INSERT INTO second_table (id, text_value)
SELECT id, 'premium' 
FROM first_table;

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269883

Are you looking for insert . . . select or create table as?

insert into table2 (id, value)
    select id, 'premium'
    from table1;

or:

create table table2 as
     select id, 'premium' as value
     from table1;

Upvotes: 1

Related Questions