Solved

WHERE fieldname IN (SELECT fieldname FROM #temptable)?

Posted on 2008-09-29
6
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22600729
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
ID: 22600788
Hi there.  It does appear where expected DIRECTLY after each entry...OCDGB< and PRDGB<
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22600831
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22600935
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
ID: 22601316
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

740 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