Solved

WHERE fieldname IN (SELECT fieldname FROM #temptable)?

Posted on 2008-09-29
6
219 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
Industry Leaders: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

752 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