Solved

Transact-SQL Statement.

Posted on 1998-12-01
4
223 Views
Last Modified: 2010-03-19
I have the following structure of 3 tables with the key fields shown.
TableA        TableB          TableC
TA_Key <----> TA_Key <------> TA_Key
              TB_Year         TB_Year
with the tables linked as shown.
In order to set up a text file with details from the three
tables I have set up a stored proc with the following statement.
SELECT * from TableA, TableB, TableC
WHERE TableA.TA_Key = @p1 AND TableA.TA_Key = TableB_TA_Key
AND TableA.TA_Key = TableC.TA_Key.

This returns me resultsets perfectly fine except in the
situations where a row exists on TableA and TableB but not
on TableC or some combination of this.
In this situation I would like the resultset to return me
the TableA and TableB rows and the TableC row as blank.
Whats the best way to write my SQL Statement ?.
ED.
0
Comment
Question by:eryan
  • 2
4 Comments
 
LVL 2

Expert Comment

by:odessa
ID: 1092022
Try to use some like "TA_Key = NULL" in this way

SELECT * from TableA, TableB, TableC
WHERE ((TableA.TA_Key = @p1) or (TableA.TA_Key = NULL)) and
      ((TableB.TA_Key = @p1) or (TableB.TA_Key = NULL)) and
      ((TableC.TA_Key = @p1) or (TableC.TA_Key = NULL))
0
 

Author Comment

by:eryan
ID: 1092023
Odessa,
That still only returns me a result set if tablesA,B,C all have
a matching Key.
I.E If there is a TableA row with key = 12
                a TableB row with key = 12, 1998
                a TableC row with key = 12, 1998 its fine
but if the tableC row does not exist, then a blank resultsset
is returned.
ED

0
 
LVL 2

Accepted Solution

by:
tschill120198 earned 70 total points
ID: 1092024
You need to use outer joins to tables B and C...

    select  a.TA_Key,
          b.year,
            c.year
    from    tableA a
            left outer join tableB b on (a.TA_Key = b.TA_Key)
            left outer join tableC c on (a.TA_Key = c.TA_Key)
    where   a.TA_Key = @p1

This will give you back nulls for tables B and C when they don't have a value... if you really want a "blank" you can convert the column in the result
0
 

Author Comment

by:eryan
ID: 1092025
Tschill,
Thanks for that, exactly what I needed.
ED.

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

Suggested Solutions

Title # Comments Views Activity
SSIS I need to get the most old file from a folder in the SSIS package. 3 23
Attaching Database Failed ? 3 38
sql server insert 12 30
Help in Bulk Insert 9 30
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

808 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