Cobine Multiple inserts into one

Database
Enthusiast

Cobine Multiple inserts into one

Hi!

I have 3 inserts queries with 2 columns harcoded. How do I merge them into one insert and would it be more efficient?
Insert into table 1
( sel col1,'Class', Class from table2)
Insert into table 1
( sel col1,'Teacher', Teacher from table2)
Insert into table 1
( sel col1,'School', School from table2)

Thanks!
1 REPLY
Teradata Employee

Re: Cobine Multiple inserts into one

try it with an UNION ALL and INSERT INTO AS SELECT .... statement, something like that:

INSERT INTO table1 (COLNAME_1,COLNAME_2,COLNAME_3) AS
SELECT SV.A, SV.B, SV.C FROM
(
sel col1 A,'Class' B, Class C from table2
UNION ALL
sel col1 A,'Teacher' B, Teacher C from table2
UNION ALL
sel col1 A,'School' B, School C from table2
) SV

You have to consider:
1- The type of all three columns Class, Teacher and School from table2 has to be the same.
2- COLNAME_1,COLNAME_2,COLNAME_3 for table1 has to be replaced with the required ones.