Solved

WHERE fieldname IN (SELECT fieldname FROM #temptable)?

Posted on 2008-09-29
6
206 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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
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
Viewers will learn how the fundamental information of how to create a table.

856 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