Solved

Creating a SQL table from a Union Query

Posted on 2014-10-01
5
106 Views
Last Modified: 2014-10-02
The following is a query I am trying to write to move the Union of 6 views into one table:
insert into livedb.dbo.tbl_union
 
select  FLD12,FLD13,FLD14,FLD15,FLD16,FLD17,FLD18,FLD19,
FLD20,FLD21,FLD22,FLD23,FLD24,FLD25,FLD26,FLD27,FLD28,FLD29,
FLS30,FLD31,FLD32,FLD33,FLD34,FLD35,FLD36,FLD37,FLD38,FLD39,
FLD40,FLDA,FLDB,FLDC,FLDD,FLDE,FLDF,FLDG,FLDH,FLDI

from
(
SELECT     *
FROM         dbo.vw_EmpDirDep_Change
UNION
SELECT     *
FROM         dbo.vw_EmpPTO_Change
UNION
SELECT     *
FROM         dbo.vw_EmpStatus_Change
UNION
SELECT     *
FROM         dbo.vw_EmpTax_Change
UNION
SELECT     *
FROM         dbo.vw_EmpWH_Change
)

Open in new window


I am getting an error that the ")" on line 24 is an incorrect syntax.

What do I need to do to correct this?

Thanks

Glen
0
Comment
Question by:GPSPOW
  • 3
5 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40356292
Add a letter a after the closing bracket, like this:
insert into livedb.dbo.tbl_union
 
select  FLD12,FLD13,FLD14,FLD15,FLD16,FLD17,FLD18,FLD19,
FLD20,FLD21,FLD22,FLD23,FLD24,FLD25,FLD26,FLD27,FLD28,FLD29,
FLS30,FLD31,FLD32,FLD33,FLD34,FLD35,FLD36,FLD37,FLD38,FLD39,
FLD40,FLDA,FLDB,FLDC,FLDD,FLDE,FLDF,FLDG,FLDH,FLDI

from
(
SELECT     *
FROM         dbo.vw_EmpDirDep_Change
UNION
SELECT     *
FROM         dbo.vw_EmpPTO_Change
UNION
SELECT     *
FROM         dbo.vw_EmpStatus_Change
UNION
SELECT     *
FROM         dbo.vw_EmpTax_Change
UNION
SELECT     *
FROM         dbo.vw_EmpWH_Change
) a

Open in new window

Explanation: SQL Server wants an alias for your subquery
0
 
LVL 25

Expert Comment

by:jogos
ID: 40356296
That query between () needs an alias

select u1.* from
(select ....
) as u1

Open in new window


And I hope this is a one time operation and you are realy sure all columns are in same order in each view.  Not same amount of columns in view will give error.  But when for example firstname and lastname switch in column-position no error will be given and values are in wrong place.

So naming each column in each part of the union is much safer and here just a copy/paste job.

Adding a column-list to the insert is also from same "better safe than sorry"
insert into livedb.dbo.tbl_union
   ( FLD12,FLD13,FLD14,FLD15,FLD16,FLD17,FLD18,FLD19,
   FLD20,FLD21,FLD22,FLD23,FLD24,FLD25,FLD26,FLD27,FLD28,FLD29,
   FLS30,FLD31,FLD32,FLD33,FLD34,FLD35,FLD36,FLD37,FLD38,FLD39,
   FLD40,FLDA,FLDB,FLDC,FLDD,FLDE,FLDF,FLDG,FLDH,FLDI)
select  FLD12,FLD13,FLD14,FLD15,FLD16,FLD17,FLD18,FLD19,
FLD20,FLD21,FLD22,FLD23,FLD24,FLD25,FLD26,FLD27,FLD28,FLD29,
FLS30,FLD31,FLD32,FLD33,FLD34,FLD35,FLD36,FLD37,FLD38,FLD39,
FLD40,FLDA,FLDB,FLDC,FLDD,FLDE,FLDF,FLDG,FLDH,FLDI

from
(
SELECT     FLD12,FLD13,FLD14,FLD15,FLD16,FLD17,FLD18,FLD19,
FLD20,FLD21,FLD22,FLD23,FLD24,FLD25,FLD26,FLD27,FLD28,FLD29,
FLS30,FLD31,FLD32,FLD33,FLD34,FLD35,FLD36,FLD37,FLD38,FLD39,
FLD40,FLDA,FLDB,FLDC,FLDD,FLDE,FLDF,FLDG,FLDH,FLDI
FROM         dbo.vw_EmpDirDep_Change
UNION
select  FLD12,FLD13,FLD14,FLD15,FLD16,FLD17,FLD18,FLD19,
FLD20,FLD21,FLD22,FLD23,FLD24,FLD25,FLD26,FLD27,FLD28,FLD29,
FLS30,FLD31,FLD32,FLD33,FLD34,FLD35,FLD36,FLD37,FLD38,FLD39,
FLD40,FLDA,FLDB,FLDC,FLDD,FLDE,FLDF,FLDG,FLDH,FLDI
from ....
) as u1

Open in new window

0
 
LVL 25

Expert Comment

by:jogos
ID: 40356301
Sorry, attention to other things before answering this question. The problem was already mentioned by chaau
0
 

Author Closing Comment

by:GPSPOW
ID: 40356320
Thank you

I have a binary error now, but I will figure that out.  One of the views is not set up like the others.

Glen
0
 
LVL 25

Expert Comment

by:jogos
ID: 40358740
" One of the views is not set up like the others."
Exactly my point why you should name your columns.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question