Solved

WHERE fieldname IN (SELECT fieldname FROM #temptable)?

Posted on 2008-09-29
6
181 Views
Last Modified: 2012-06-27
Please can someone help me here.  I am sure this is quite obvious to most SQL people but I cannot work out what is going wrong!!!!

I have a T-SQL script that creates a temporary table...

CREATE TABLE #TempContainers
(
DRA_Container varchar(255)
)

It then uses an INSERT command to fill it with multiple records.

Now, the following statement works as expected...

SELECT DRA_Container FROM #TempContainers

This gives me two records (PRDGB and OCDGB).

However, when I issue the following command...

SELECT * FROM DRA_Totals WHERE DRA_Container IN (SELECT DRA_Container FROM #TempContainers)

I only get the records from DRA_Totals with one of the DRA_Containers in #TempContainers and not the other one.  Incidentally, if I don't use a SELECT in the brackets but specify the values (e.g. IN ('value1','value2')), it picks up both correctly.

Is this a problem with temporary tables or something else I have overlooked?  Tried EXISTS too but don't think I fully understand how to get what I need :-)

Many thanks,
Tony
0
Comment
Question by:antdow
  • 3
  • 3
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
your code looks 100% fine.

question: where is the < appearing in the output if you do this:
SELECT DRA_Container + '<' FROM #TempContainers

Open in new window

0
 
LVL 1

Author Comment

by:antdow
Comment Utility
Hi there.  It does appear where expected DIRECTLY after each entry...OCDGB< and PRDGB<
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
what is the data type of DRA_Container in DRA_Totals?

and there is no reason why it would only take 1 of the values from a temporary table.
there must be indeed something else that gets overlooked...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:antdow
Comment Utility
Weird then!!!  Very strange.  Is varchar(20) and definintely not working.  I'll keep looking,  thanks for your time.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
could you "feed" me with a script to create the table you query, how you "fill" the temporary table?
so I could try to reproduce the problem?
0
 
LVL 1

Author Comment

by:antdow
Comment Utility
Hi Angellll.  I have now solved this.  Appears it was a datatype issue - when populating the lookup table with the INSERT.  Should have spotted it so sorry for wasting your time.  The points are yours though for taking time to respond, and so quickly too.  Thanks again.  Thought I was going mad!!!

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now