Solved

WHERE fieldname IN (SELECT fieldname FROM #temptable)?

Posted on 2008-09-29
6
191 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]
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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

929 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

15 Experts available now in Live!

Get 1:1 Help Now