Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4623196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:57:06+00:00 2026-05-22T02:57:06+00:00

I have a stored procedure that currently uses one CTE. This one works like

  • 0

I have a stored procedure that currently uses one CTE. This one works like so:

WITH MY_CTE AS
(
    // Logic here uses SELECT * from a single table.
)
SELECT *
INTO #Tasks
FROM MY_CTE;

I now have a requirement to optionally call another CTE that will add more data to the original temp table. I was hoping to do something like this:

IF @Option = 1
    BEGIN
        INSERT INTO #Tasks
        (
            WITH MY_OTHER_CTE
            (
                // Logic here uses SELECT * from same table as first CTE.
            )
            SELECT *
            FROM MY_OTHER_CTE
        )
    END

The problem is that the INSERT INTO #Tasks call requires specifying VALUES columns. Both CTE’s return records from the same table. The good benefit of original stored procedure was that it would work even if columns in that table changed, since I was using SELECT *. I could specify the columns knowing what they are in the shared table, but I lose that benefit (which in this specific case is an extremely useful benefit). Is there a way to accomplish what I’m trying to do, knowing that they are both selecting from the same table and that the columns will always match?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:57:07+00:00Added an answer on May 22, 2026 at 2:57 am

    Does this work?

    ;WITH MY_OTHER_CTE AS
    (
        // Logic here uses SELECT * from same table as first CTE.
    )
    INSERT INTO #Tasks
        SELECT *
        FROM MY_OTHER_CTE
    

    You might have another challenge if the table you’re selecting from (and hence the temp table) has an identity column

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that has this line: SET @SQL = 'SELECT path,title,tags
I currently have a stored procedure that copies content from one table to another.
I have this stored procedure that I want to use for my SAP crystal
I have a stored procedure (that I didn't write) that uses openquery to populate
I have a stored procedure that logs some data, how can I call this
I currently have PHP code that handles the logic for this because I do
I have a stored procedure that uses a view to pull 6 averages. The
Hello All I am using infragsitics tree view control.I have one stored procedure that
Helo, My question is I have one Stored Procedure in SQL Server that returns
I am currently using MS SQL Server 2008. I have a stored procedure that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.