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

  • SEARCH
  • Home
  • 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 7668887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:28:15+00:00 2026-05-31T15:28:15+00:00

I was doing some querying today in T-SQL, SQL-Server-2008 and stumbled upon something weird

  • 0

I was doing some querying today in T-SQL, SQL-Server-2008 and stumbled upon something weird that I didn’t understand. Using the query windows, I am trying to query from two common table expressions like so (I stripped out a lot of code to make it more obvious what I was doing):

;WITH temp1 AS (SELECT * FROM dbo.Log)
, temp2 AS (SELECT  * FROM dbo.SignalCodeItems300_tbl)

SELECT * FROM temp1
SELECT * FROM temp2

However, only one of the select statements will run, the FIRST one. Regardless of which is which, only the first runs. I assume this is some sort of syntax thing that I’m missing maybe? I get the error “Invalid object name ‘temp2′”.

Could someone shed some light on this problem? Are there any workarounds for this?

  • 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-31T15:28:17+00:00Added an answer on May 31, 2026 at 3:28 pm

    No, this works as it should. A CTE (Common Table Expression) is only available for the first statement after the definition. So in other words, after select * from temp1, they both become unavailable.

    The fix would be this:

    ;WITH temp1 AS (SELECT * FROM dbo.Log)
    SELECT * FROM temp1
    
    ;WITH temp2 AS (SELECT  * FROM dbo.SignalCodeItems300_tbl)
    SELECT * FROM temp2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008. I have some dates in my database that
While doing some refactoring I've found that I'm quite often using a pair or
Been doing some playing call my service which is on a different domain using
im doing some practice sql problems one of the questions is: Prepare a shipping
While doing some small regex task I came upon this problem. I have a
I am directly querying the back-end MS SQL Server for a software package. The
I'm trying to do quite a lot of querying against a Microsoft SQL Server
I am using ZODB to store some data that exists in memory for the
I have some PL/SQL code that I think might have a memory leak. Everytime
I am doing some loading from an oracle db, using ODP.NET. In its current

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.