Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005
    Posts
    16

    Unanswered: How to Append Multiple Queries to a Temp Table?

    Hello. I'm having some difficulty trying to output the results of two seperate queries into the same temporary table.

    Does anyone know if it is possible to use the UNION operator to output the results into a temporary table?

    Code:
    Select * From TableA
    UNION
    Select * From TableB
    <---output result to temporary table here--->
    Alternatively, is it possible to output the results of two queries in to the same temporary table without the UNION clause?

    The following statement fails on the second SELECT INTO due to the fact that #MyTempTable already exists.

    Code:
    Select * INTO #MyTempTable FROM TableA
    Select * INTO #MyTempTable FROM TableB
    Thanks in advance.
    Last edited by Krispy; 04-04-06 at 08:02.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I think if you specify the SELECT * INTO in the top half of your union it ought to do the trick.

    Yes this works...

    Code:
    USE NORTHWIND
    GO
    SELECT * INTO EMPLOYEES2
    FROM EMPLOYEES
    UNION ALL
    SELECT * FROM EMPLOYEES
    Points deducted for style though, never use SELECT * in production. Need a column list, do this...

    Code:
    SELECT COLUMN_NAME + ', '
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'Employees'
    ORDER BY ORDINAL_POSITION
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Nov 2005
    Posts
    16
    Thanks Thrasymachus - that's exactly what I was looking for.

    ...and I'm sure you'll be pleased to hear the SELECT * was only for the example's in this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •