Select Into in From Clause Query

  • Hi all

    Can some one please tell me if i can use a select into in a From Clause Sub Query or a Insert into in the From clause query.

    I have a complex query with From Clause Queries in it using a Union all..

    I am trying to insert it into a table can you please let me know if this is possible and if so how

    thanks in advance

    Vani

    E.g.

    Select A, B, C from (Select A1, B1, C1 from (Select A2, B2, C2 from Table1 Union Select E2, F2, G2 from Table1)

    and rest of the code

    Sorry about the complex e.g.

  • The INTO clause goes before the first FROM:

    Select A, B, C

    into YourNewTable

    from (Select A1, B1, C1 from (Select A2, B2, C2 from Table1 Union Select E2, F2, G2 from Table1)

    Your question sounds like you're trying to select into the new table from the subquery. If you think about this for a moment, you'll realize why this can't be done... if you insert into a new table, the subquery won't be returning any data to the query calling the subquery. If that is what you really want, you'll need to break it out into separate statements:

    Select A1, B1, C1

    INTO YourNewTable

    from (Select A2, B2, C2 from Table1 Union Select E2, F2, G2 from Table1)

    Select A, B, C from YourNewTable

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Hi Wayne

    Thanks for ur help... I got it now

    Cheers for your help 🙂 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

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