0

I would like to insert a few data into a table selecting data from a subquery having multiple joins.

I tried the following way:

insert into mytable (columns) 
select * from 
 (select columns 
  from tableA 
  UNION ALL 
  select columns from tableB 
  UNION ALL 
  select columns from tableC)

Can you please suggest me a way out, since I am getting the following error while executing the above query.

Incorrect syntax near ')'.

  • 1
    give the alias to your subquery, i.e. ")t" instead of ")" at the end of youe query – sepupic Oct 19 '18 at 7:24
  • 1
    @sepupic, thanks for the help. It really worked – l.lijith Oct 19 '18 at 7:44
3

Add some alias:

insert into mytable (columns) 
select t.* from (
    select columns from tableA 
    UNION ALL
    select columns from tableB
    UNION ALL
    select columns from tableC
) t
1

Simply remove select * from ( and trailing ) from your query - it is excessive.

insert into mytable (columns) 
select columns from tableA 
UNION ALL 
select columns from tableB 
UNION ALL 
select columns from tableC

fiddle

Not the answer you're looking for? Browse other questions tagged or ask your own question.