8

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
  • 1
    Oracle doesn't track it because there is no such relationship, except by convention. This is why you usually use some kind of naming convention, e.g. table XYZ has sequence SEQ_XYZ. Commented Mar 3, 2010 at 4:08

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you have sequence_name and you want to check for which tables it is being used via triggers you can use above query with one more "and" condition specifying name of sequence : 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' and ud.referenced_name = 'YOUR_SEQ_NAME';
2

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.

2 Comments

This will not take care of cases like "SELECT MYSEQ . NEXTVAL FROM dual;"
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!
2

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.

1 Comment

If stored procedure sp_something references sequence seq_something, then sp_something will surely appear in seq_something's "referenced by" list?
1

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

3 Comments

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.
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 it's 9i. But anyways, it's good to know about REGEX_LIKE. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.