Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following scenario for which i will have to write a stored procedure:

Header table containing invoice_ID and invoice_line_ID
Address Line table containing invoice_line_id and 'Ship_From' and 'Ship_To' corresponding to each invoice_line_ID in header table.
3.Address header table containing invoice_ID and 'Ship_From' and 'Ship_To' corresponding to each invoice_id in header table.
The cases are such that not always all 'Ship_From' and 'Ship_To' information will be present in the Address Line table. In that case the information needs to be selected from Address Header table.

So i will write a case structure and two joins : 1. That will join Header table and Address Line table 2. That will join Header table and Address Header table. with the condition to do the second join in case entire information for a particular invoice_line_id is not available in line table.

My question here is where should i store the information? I will use a cursor to perform the above case structure. But should i use a ref cursor or a temp table in this case?

Please note that my customer is not liking the idea of extra database objects in the database so i might have to delete the temp table after i am done displaying. I need help on that as well as to is there any alternative to temp table or whether ref cursor take up extra space on the database or not.
Posted
Updated 5-Nov-14 11:56am
v2
Comments
[no name] 5-Nov-14 17:49pm    
You wrote: "Header table containing invoice_ID and invoice_line_ID"

Header table with a something like Details makes absolutely no sense.
Sormita 5-Nov-14 17:51pm    
Actually header table contains some other information related to transaction but the tables that i want to join with do not have any relevance with that information.
[no name] 5-Nov-14 17:52pm    
Not clear. Maybe better to improve question with some fragments of table structures.
Sormita 5-Nov-14 17:56pm    
what are you not clear about?i can explain it here-i dont know what other information to add to the question.
[no name] 5-Nov-14 18:00pm    
ok. It is not my problem if you think "Header table containing invoice_ID and invoice_line_ID" makes sense then go on. Sorry that I asked back.

1 solution

There is no simple answer, because it depends on many factors.
Please, see: Oracle: Complex recordsets[^] and Creating and Using Temporary Tables in Oracle[^]

In your case i'd suggest to use simple SELECT statement together with suitable JOINs[^] AND COALESCE[^] function. COALESCE function is used to replace nulls with default values.
Finally, your query might look like:
SQL
SELECT HT.<Field_List>, COALESCE(AL.ShipFrom, AH.ShipFrom) AS ShipFrom, COALESCE(AL.ShipTo, AH.ShipTo) AS ShipTo
FROM HeaderTable AS HT LEFT JOIN AddressLineTable AS AL ON HT.invoice_line_id = AL.invoice_line_id
    LEFT JOIN AdressHeaderTable AS AH ON HT.invoice_line_id = HT.invoice_line_id


As you can see, in case when AL.ShipFrom will be NULL, it should be replaced with AH.ShipFrom.

Note: i have no idea about your data structure, so the query have to be changed to your needs.

More useful information about JOINs, you'll find here: Visual Representation of SQL Joins[^]
 
Share this answer
 
Comments
Manas Bhardwaj 6-Nov-14 16:38pm    
Yes, it depends. There is no one size fits all. +5!
Maciej Los 6-Nov-14 16:53pm    
Thank you, Manas ;)
Sormita 7-Nov-14 12:03pm    
thanks maciej for the solution, my problem is when the information is not there in the table, there is no record at all, so i cannot check for null value.I tried to use UNION but i am getting some undesirable results with that. So finally i am fetching things separately and handling the rest of the logic at front end.
Maciej Los 7-Nov-14 12:51pm    
Have you tried my solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900