Reputation: 11
Ok this is what I'm trying to do. Let's say I have several views each view can have aliases and such like that and each view goes back to either another or view or a table. What I want to do is trace a specific field from it's view all the way down to the source. For example if I have this sql statement:
replace view blah
Select
t.acct_nmbr as I_account,
sum (t.num1+p.location) as location,
from blahTable as t
left outer join blahTable2 p
on t.acct_nmbr=p.acct_nmbr;
This is a very simple case but what I want it is if I say trace(i_account) I will get the following hierarchy:
I_account --> blahTable ---> acct_nmbr
Or if I want location I would get the following:
location --> sum (t.num1+p.location)-->blahTable --> num1
------------------------------------------------>blahTable2 --> location
You can see that adding more and more it can get complicated to trace this especially if there is multiple joins and select and derived tables as well. I'm currently trying to code this from scratch but I wasn't sure if there was something out there already that does this.
Upvotes: 0
Views: 738
Reputation: 13056
I think this is an interesting concept. Ordinarily, I'd probably try to walk this recursively through a tree structure (which is what this is).
However, you're going to run into a couple of problems really quick;
CASE
statements that switch fields for evaluation. You haven't listed your RDBMS, but some of them support multiple columns in some aggregates (like DB2 does for MAX()
). This is going to cause problems because your destination column is dependent on the data retrieved.Alias
, which basically repoints where a table (or view reference) actually points. This will have the effect of changing what 'base' table you're looking at (from an SQL standpoint). Depending on how you're getting your data, your Java code may resolve the alias or not. In either case, an alias is usually temporary, which has the potential to 'expire' your results.So, in short, for anything like a real-world scenario, it's pretty much impossible...
5.How are you planning on handling recursive CTEs (I'm assuming teradata allows them)? This is something of a wierd subset of point 2; any fields in them either resolves to a 'base table' (what the optimizer sees as a table anyways), or a recursion towards the base table. You would need to design your program to detect recursion of this type, and deal with it appropriately. Unfortunately, you won't be able to rely on the terminating condition (evaluated per-line at runtime); thankfully, however, you won't have to deal with cyclical relationships.
Upvotes: 1