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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:12:50+00:00 2026-05-24T18:12:50+00:00

I have found the following issue in SQL Server 2008 Management Studio. When I

  • 0

I have found the following issue in SQL Server 2008 Management Studio. When I run the following script as whole I expect errors (due to a “copy and paste error”), but don’t receive them.

IF OBJECT_ID('Foo') IS NOT NULL
BEGIN
    DROP TABLE Foo
END
IF OBJECT_ID('Bar') IS NOT NULL
BEGIN
    DROP TABLE Bar
END
CREATE TABLE Foo (
    FooID int
)
Create Table Bar (
    BarID int
,   FooID int
)
INSERT INTO Foo
SELECT 1 UNION ALL SELECT 2
INSERT INTO Bar
SELECT 1,1 UNION ALL SELECT 2,1 UNION ALL SELECT 3,1
GO

IF OBJECT_ID('tempdb..#temp') IS NOT NULL
BEGIN
      DROP TABLE #TEMP
END
GO
CREATE TABLE #TEMP (
    FooID int
)
INSERT INTO #TEMP
    SELECT FooID FROM Bar
GO
SELECT * FROM Foo WHERE FooID IN (SELECT FooID FROM #TEMP)
GO
SELECT * FROM Bar WHERE BarID IN (SELECT BarID FROM #TEMP)
GO
SELECT * FROM #TEMP
GO

The second last statement containing the where clause filter on “SELECT BarID FROM #TEMP” runs, but there is no BarID column in #TEMP. When running the script as a whole I receive no error and the script return all rows in Foo. However when I run the command on its own then I get the error informing me that there is no BarId in #TEMP.

Is there a reason for this? Is it my code?

  • 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-24T18:12:51+00:00Added an answer on May 24, 2026 at 6:12 pm

    Your second to last query actually is working correctly.

    Other commenters please correct me, but I understand this to be a correlated subquery. Table aliasing will show what is actually going on. Your query is equivalent to this:

    SELECT * FROM Bar x WHERE x.BarID IN (SELECT x.BarID FROM #TEMP)
    

    For each row in #TEMP, the nested select is actually returning the current value of BarID from table Bar, so yes, BarID in (BarID) is true, so every row in Bar is matched, so every row is returned.

    To show that you’re not crazy, try

    SELECT * FROM Bar WHERE BarID IN (SELECT NonExistentFieldName FROM #TEMP) 
    

    This raises the error I think you’re expecting.

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

Sidebar

Related Questions

I have been using SQL Server Management Studio for years. Yesterday, when I started
i have an instance of SQL server 2008 and an instance of SQL server
I have done my best to install a copy of SQL Server 2008, but
I have upgraded from SQL Server 2005 to 2008. I remember that in 2005,
I have an weird issue, I am running the following SQL statement: SELECT *
On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
I have found the following command in AWK useful in Vim :'<,'>!awk '{ print
I have found the following expression which is intended to modify the id of
I have found the following resources on Balanced Matching for .net Regexes: http://weblogs.asp.net/whaggard/archive/2005/02/20/377025.aspx http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx
Reading a book I have found the following statement: (Object) Behaviors answer either of

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.