472,348 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

Combining INTO with UNION

What's the syntax for combining INTO and UNION clauses?

What I want to do is:

SELECT blah
INTO newtable
FROM oldtable1
WHERE <conditions>
UNION
SELECT blah
INTO newtable
FROM oldtable2
WHERE <conditions>

DTS doesn't seem to like my syntax. Where should the INTO(s) really go?

Thanks

Andy

Nov 23 '05 #1
5 33816
MC
Try with something like this:

select <column list>
into newtable
from
(
select <column list>
from table1

union

select <column list>
from table2
) un
MC
"Andy Kent" <an******************@virgin.net> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
What's the syntax for combining INTO and UNION clauses?

What I want to do is:

SELECT blah
INTO newtable
FROM oldtable1
WHERE <conditions>
UNION
SELECT blah
INTO newtable
FROM oldtable2
WHERE <conditions>

DTS doesn't seem to like my syntax. Where should the INTO(s) really go?

Thanks

Andy

Nov 23 '05 #2
Thanks, that worked...

Next part of problem: how would I get it to create and populate an
IDENTITY column?

Bearing in mind I'm working in DTS, which doesn't like anything too
clever ...

Nov 23 '05 #3
MC
well, you can add identity column after you insert data. So, after insert
issue:
go
alter table table1
add Something_ID int identity(1,1)

LMK if you need anything else

MC

"Andy Kent" <an******************@virgin.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks, that worked...

Next part of problem: how would I get it to create and populate an
IDENTITY column?

Bearing in mind I'm working in DTS, which doesn't like anything too
clever ...

Nov 23 '05 #4
Stu
A poor carpenter blames his tools.

Not trying to pick on you, but DTS is very powerful; there are some
things that it's not good at, but chewing up CORRECT SQL syntax is not
one of them.

If you want to add an identity column in one step, you can use a
subquery for the UNION statement:

SELECT SELECT IDENTITY(int,1,1) as splat, blah
INTO newTable
FROM (SELECT blah
FROM oldtable
UNION --do you need DISTINCT values? UNION ALL will be faster
SELECT blah
FROM oldTable2) x

Stu

Nov 23 '05 #5
You were quite close... Here's an example:

Use Northwind
GO

SELECT OrderID
INTO #t
FROM Orders
UNION
SELECT ProductID
FROM "Order Details"

HTH,
Gert-Jan
Andy Kent wrote:

What's the syntax for combining INTO and UNION clauses?

What I want to do is:

SELECT blah
INTO newtable
FROM oldtable1
WHERE <conditions>
UNION
SELECT blah
INTO newtable
FROM oldtable2
WHERE <conditions>

DTS doesn't seem to like my syntax. Where should the INTO(s) really go?

Thanks

Andy

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alan | last post by:
I'm just about to start a project that needs to combine the results of a SQL Server query with the results of an Index Server query. The basic idea...
15
by: Phillip Rhodes | last post by:
Hi all, I have a question I hope someone can help me with: I'm doing some communication over a socket, and have need to send and receive values...
1
by: PATGMorris | last post by:
I've got a Union Query that pulls data from two different tables for chemistry and micro testing. The tables containing very similar data but for...
4
by: justin tyme | last post by:
Hello Experts! I would like to combine (which may not be the correct technical term) two text fields from the same table in a query....
2
by: m.k.ball | last post by:
Hi - I have some tables designed to keep track of order. The main table contains a session id, address and date. Linked to this I have a table of...
7
by: Frank | last post by:
Hi there, I'm trying to generate a report for an old database and I'm having trouble coming up with an elegant way of going about it. Using...
2
by: spatik | last post by:
I have a scenario like this in my application. We can have values documented for each time column. I have discrete timepoints in hand and I need to...
3
by: billelev | last post by:
I am trying to combine two queries into one UNION query. The first sub-query works, but the second creates the following error at the end of the...
2
by: Don Barton | last post by:
I have 2 tables, Table 1 has Name, NameID, and A, B, C fields. Table 2 has Name, NameID, and D, E fields. Several of the Names/NameID are the same...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.