Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Returnig records on EXIST

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2007-01-02 : 17:50:24
I have two tables: A and B. A is the main table and B is the exception table. What I need to do is check if anything in Table A exists in table B based on a couple of fields.

Ex:
                                                                                                                                                                       SELECT [sCustomerFullName], [sAccountNumber], [sSIM], [sPlanCode]FROM A a                                                                                                                                                                    WHERE EXISTS  (SELECT * FROM B b  WHERE a.ssim = b.sSim AND a.sTMobilePlanCode = b.sTMobilePlanCode)


Let's say Table A contains custname(Joe Shmoe), but the sSIM is empty (because the table doesn't allow NULL values). This customer DOES NOT exist in table B. This record shouldn't be returned because it doesn't exist in table B, but it is being returned because the sSIM is empty.

I played with my own copy of the table A and set up sSIM as allowing NULL values. Using the query above did not return the record.

Does anyone know how to get around this if the field does not allow NULLs?

Thanks,
Ninel





SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-02 : 17:55:01
Some sample data for the two table, and your expected output would be nice.
SELECT		a.[sCustomerFullName],
a.[sAccountNumber],
a.[sSIM],
a.[sPlanCode]
FROM Table1 AS a
INNER JOIN Table2 AS b ON b.sSim = a.sSim AND b.sTMobilePlanCode = a.sTMobilePlanCode


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -