Combining Temp Table Results

  • Hello, I have multiple temp tables that return one row of results each. What would be the best way to combine these results into one giant temp table. I tried using UNION and UNION ALL after each drop table but that didn't work. If anyone has a suggestions, please let me know. Thanks!

  • Union all should have worked but it's impossible to tell without seeing your code, at the very least.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks! I feel a union all would work too but it just doesn't. I have temp tables and I think that is what is messing it up. Here is what I have so far:

    Select columns

    into #tableA

    from

    where

    order by

    with

    as (select

    from #tableA

    group by)

    select

    from

    group by

    drop table #tableA

    union all

    Select columns

    into #tableB

    from

    where

    order by

    with

    as (select

    from #tableB

    group by)

    select

    from

    group by

    drop table #tableB

    I get an error that says "Incorrect syntax near the keyword 'union'." I'm not sure of any other way to structure this.

  • You have your syntax wrong. Additionally, CTEs won't work on SQL Server 2000 and previous.

    For 2005+ this syntax would work.

    Select columns

    into #tableA

    from SometableA

    where SomeCondition

    --order by --unnecessary

    ;

    Select columns

    into #tableB

    from SomeTableB

    where SomeCondition

    --order by --unnecessary

    ;

    WITH

    CTE1 AS (

    select columns, aggregation

    from #tableA

    group by columns

    ),

    CTE2 AS (

    select columns, aggregation

    from #tableB

    group by columns)

    SELECT somecolumns, aggregation

    FROM CTE1

    GROUP BY somecolumns

    UNION ALL

    SELECT somecolumns, aggregation

    FROM CTE2

    GROUP BY somecolumns;

    DROP TABLE #tableA;

    DROP TABLE #tableB;

    It seems that you're creating many unnecessary steps. This should give the same result with less work:

    SELECT columns, aggregation

    FROM SometableA

    WHERE SomeCondition

    GROUP BY columns

    UNION ALL

    SELECT columns, aggregation

    FROM SomeTableB

    WHERE SomeCondition

    GROUP BY columns

    ORDER BY somecolumn;--if needed

    All the code posted here is pseudocode and won't work as it is.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Awesome, that worked! Thank you so much!

  • Hopefully, you did understand why were you getting an error and how the syntax works. CTEs and UNION (ALL) are part of a single statement and can't be divided by, for example, a drop table statement.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Yes, got it, thank you!

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply