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 7705403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:51:27+00:00 2026-05-31T23:51:27+00:00

I think I may misunderstand why the with command is used. But can any

  • 0

I think I may misunderstand why the with command is used. But can any one see what I am doing wrong.
I want to do a query and use the results for two things. First I want to use the values to make some inserts into another table. Then I want to display the results to the user.

So I have something like this.

With temp as (
Select * from Table1 
)
INSERT INTO Table2 (table1_id) select id from temp
SELECT * from temp 

And I get

Error: Invalid object name ‘temp’. SQLState: S0002
ErrorCode: 208

Is this not what the with command is for?

  • 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-31T23:51:29+00:00Added an answer on May 31, 2026 at 11:51 pm

    From MSDN:

    A common table expression (CTE) can be thought of as a temporary
    result set that is defined within the execution scope of a single
    SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement.

    Since the insert statement and the select statement are two different statements, the CTE is only valid for the insert.

    As an alternative, you could consider using an OUTPUT clause:

    WITH temp AS (
        SELECT * FROM Table1 
    )
    INSERT INTO Table2 (table1_id) 
    OUTPUT inserted.id
    SELECT id FROM temp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this may sound pretty simple, but still I can not get it
You may think this question is like this question asked on StackOverflow earlier. But
This isn't quite as straight forward as one may think. I'm using a plugin
I think it may be a bug in modelformset_factory in Django 1.2, but I'd
I think this may be a very simple question, but I am only just
You may think this question is stupid or something but as a new IT
I think I may be using a wrong window style or something or maybe
I think I may have misunderstood something here... But here goes. I'm using the
This I think may be a silly question, but I have become quite confused
I've found what I think may be a bug with Ivar and Objective-C runtime.

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.