is there a simple way to make insert to a table using values from another table?
Lets say I have to fetch the NAME value from T1 and I want to make a hardcoded insertion to another table using that value.
The insertion values should be something like
INSERT INTO T2 (NAME, AGE, GENDER) VALUES (NAME, 22, 'M')
Name is fetched from T1, 22 and 'M' are hardcoded in the insert query.
Asked
Active
Viewed 66 times
1
-
1Similar: https://stackoverflow.com/questions/7323407/insert-select-statement-in-oracle-11g – Mark Rotteveel Sep 27 '18 at 18:58
3 Answers
3
Yes . . . insert . . . select
:
insert into t2 (name, age, gender)
select name, 22, 'M'
from t1;

Gordon Linoff
- 1,242,037
- 58
- 646
- 786
3
You can do a select in an insert:
INSERT INTO T2 (NAME, AGE, GENDER)
SELECT NAME, 22, 'M' FROM T1
Also handy because you can ditch the insert and just run the select to test what you are importing before you do it, and easy to add a WHERE NOT EXISTS
to make it idempotent.

Mark Rotteveel
- 100,966
- 191
- 140
- 197

JCMC
- 46
- 3
2
You can use an insert-select statement, where you select literals for the values you want:
INSERT INTO t2
SELECT name, 22, 'M'
FROM t1

Mureinik
- 297,002
- 52
- 306
- 350