tags:

views:

2148

answers:

5

In SQL Server this inserts 100 records, from the Customers table into tmpFerdeen :-

SELECT top(100)*
INTO tmpFerdeen
FROM Customers

Is it possible to do a SELECT INTO across a UNION ALL SELECT :-

SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas

Not too sure where to add the INTO clause.

A: 

SELECT * INTO tmpFerdeen FROM (your query here) data

Chris
Msg 102, Level 15, State 1, Line 93Incorrect syntax near ')'.
Ferdeen
sorry, have updated it - you need to provide a table name for subqueries
Chris
A: 

Try something like this: Create the final object table, tmpFerdeen with the structure of the union.

Then

INSERT INTO tmpFerdeen (
SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas
)
achinda99
In SQL Server temp tables are created on the fly. I would like to do this without creating a final object table. Thanks.
Ferdeen
A: 

Maybe try this?

SELECT * INTO tmpFerdeen (
SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas)
Ryan
A: 

How about:

SELECT * INTO tmpFerdeen FROM
(SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas)
Remou
+2  A: 

This works in SQL Server:

SELECT * INTO tmpFerdeen FROM (
  SELECT top 100 * 
  FROM Customers
  UNION All
  SELECT top 100 * 
  FROM CustomerEurope
  UNION All
  SELECT top 100 * 
  FROM CustomerAsia
  UNION All
  SELECT top 100 * 
  FROM CustomerAmericas
) as tmp
Chris Pebble
Also this works SELECT top 100 * INTO tmpFerdeen FROM Customers UNION All SELECT top 100 * FROM CustomerEurope UNION All SELECT top 100 * FROM CustomerAsia UNION All SELECT top 100 * FROM CustomerAmericas (sorry can't format the sql here). Thanks!
Ferdeen