views:

231

answers:

5

Having a sequence, I need to find out which table.column gets its values. As far as I know, Oracle doesn't keep track of this relationship. So, looking up for the sequence in source code would be the only way. Is that right?

Anyone knows of some way to find out this sequence-table relationship?

+1  A: 

If your sequence is used in a trigger, the trigger will be listed in the sequence's "referenced by" list.

If your sequence is only used in the source-code queries, than yes, browsing the code is the only way.

Quassnoi
If stored procedure `sp_something` references sequence `seq_something`, then `sp_something` will surely appear in `seq_something`'s "referenced by" list?
Jaú
@Jaú: yes, it will.
Quassnoi
+1  A: 

Use GREP to scan your entire source for "myseq.NextVal" - myseq being the one you're looking for....

Martin Milan
That has been being my approach. `grep -Prine "myseq\." .` on the entire code base. Trouble is, people doesn't have all code under version control. I think there are lots of code (procedures) stored only on the database.
Jaú
If you can refer only the stored code in DB, then try `ALL_SOURCE`. If you are in 10g, use `REGEXP_LIKE` function to search.
Guru
@Guru it's 9i. But anyways, it's good to know about `REGEX_LIKE`. Thanks.
Jaú
+1  A: 

In the database you can search all stored code in your schema like this:

select type, name, line, text
from all_source
where owner = 'MYSCHEMA'
and upper(text) like '%MYSEQ.NEXTVAL%';

In SQL Developer, there is a report to do this.

Tony Andrews
This will not take care of cases like "SELECT MYSEQ . NEXTVAL FROM dual;"
jva
True, and that is always an issue with text searches. You could use '%MYSEQ%.%NEXTVAL%', but then that might produce false positives for something like 'MYSEQ2.NEXTVAL'. No easy answers for that!
Tony Andrews
+4  A: 

The problem is that Oracle allows us to use one sequence to populate columns in several tables. Scenarios where this might be desirable include super-type/sub-type implementations.

You can use the dependencies in the data dictionary to identify relationships. For instance, if you use triggers to assign the values then this query will help you:

select ut.table_name
       , ud.referenced_name as sequence_name
from   user_dependencies ud
       join user_triggers ut on (ut.trigger_name = ud.name)
where ud.type='TRIGGER' 
and ud.referenced_type='SEQUENCE'
/

If you use PL/SQL then you can write something similar for TYPE in ('PACKAGE BODY', 'PROCEDURE', 'FUNCTION'), although you will still require some trawling through the source code to assign tables and sequences when you have multiple hits.

APC
A: 

I'd like to add background information on sequences.

Robert Merkwürdigeliebe