Solved

Creating a SQL table from a Union Query

Posted on 2014-10-01
5
103 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now