473,512 Members | 15,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 33908
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
4969
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 is that the user enters/selects a bunch of search...
15
6718
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 of type double. I'm doing everything in terms of...
1
1994
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 reasons not necessary here, cannot be put into one...
4
3482
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. Specifically, text field A and text field B are both lists of...
2
1561
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 items that make up each order. I'm concerned that...
7
3032
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 cursors and other 'ugly' tools I could get the job done...
2
1695
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 find the documented values just before and just...
3
1536
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 first iif statement: "Syntax error in union query" ...
2
3849
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 in both databases. I want my merged the tables...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.