|
|
|
Re: Function that returns source tables from a query [message #598659 is a reply to message #598658] |
Wed, 16 October 2013 13:01 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Gee...OP said sql? I saw the function, thought he needs the tables used in the function. But why would need another query to see the tables used in a sql? It would be easily visible in the FROM clause.
I meant this -
SQL> create table t(a number);
Table created.
SQL>
SQL> create or replace function f
2 return number
3 as
4 v number;
5 begin
6 select count(*) into v from t;
7 return v;
8 end;
9 /
Function created.
SQL>
SQL> show errors;
No errors.
SQL>
SQL> select name,referenced_name,referenced_type from user_dependencies where
name='F' and referenced_owner='SCOTT';
NAME REFERENCED_NAME REFERENCED_TYPE
------------------------------ ---------------------------------------------------------------- ------------------
F T TABLE
|
|
|
|
|
|
Re: Function that returns source tables from a query [message #598664 is a reply to message #598662] |
Wed, 16 October 2013 13:47 |
|
mertez
Messages: 9 Registered: August 2013 Location: slo
|
Junior Member |
|
|
Actually the thing is that some of our queries are quite big and has many subselects so searching for source tables is painless. Because of that I am looking for an automated way for searching them.
So if I understand correctly there is no way to check dependencies on the fly (with an sql string) but you first have to create a view or some other object and after that check for all dependencies? Probably this would be the most logical thing to do..
It would be interesting to know how does the query planner find dependencies between tables. There is a similar problem...
ps: thanks for the solution
[Updated on: Wed, 16 October 2013 13:48] Report message to a moderator
|
|
|
Re: Function that returns source tables from a query [message #598668 is a reply to message #598660] |
Wed, 16 October 2013 14:28 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 16 October 2013 14:01Or maybe this way:
It will work, but not in all cases. View can't be created from every query. For starters view can't be created if select list expression isn't aliased:
SCOTT@orcl > create or replace
2 view v1
3 as
4 select 1
5 from dual
6 /
select 1
*
ERROR at line 4:
ORA-00998: must name this expression with a column alias
SCOTT@orcl >
Or privilege issue. User simply might not have CREATE VIEW privilege. Or user can have SELECT privilege on query table(s) but via role and therefore view can't be created. Thera are possible more cases when view can't be created.
SY.
|
|
|
|
Re: Function that returns source tables from a query [message #598708 is a reply to message #598681] |
Wed, 16 October 2013 18:18 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 16 October 2013 16:04
We can (and should) create the function with AUTHID CURRENT_USER to avoid privileges problem but the CREATE VIEW one.
Not sure how this will help. AUTHID CURRENT_USER will help to select from a table where user has SELECT privilege granted via role. It will not help to create a view on such table. CREATE VIEW prereq is directly granted SELECT privilege.
SY.
|
|
|
|
Re: Function that returns source tables from a query [message #598738 is a reply to message #598717] |
Thu, 17 October 2013 05:22 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel Cadot wrote on Thu, 17 October 2013 01:37Nope:
It is not what I said. In your example user owns table T, while I was talking about user has SELECT via role on a table in some other schema. Try the following:
1) User U1 owns table T.
2) User U2 has CREATE VIEW privilege.
3) User U2 has execute on function GET_SQL_DEPENDENCIES
4) User U2 is granted SELECT on U1.T via role R.
5) User U2 wants to find all tables involved in SELECT * FROM U1.T
SY.
[Updated on: Thu, 17 October 2013 05:29] Report message to a moderator
|
|
|
Re: Function that returns source tables from a query [message #598744 is a reply to message #598738] |
Thu, 17 October 2013 06:20 |
ThomasG
Messages: 3211 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Another option that came to mind:
Would running an explain plan for the query, and then checking the plan table work?
In object_name and object_type of the plan_table you would have either directly the tables that are used in the query, or indexes belonging to the tables used.
|
|
|
Re: Function that returns source tables from a query [message #599084 is a reply to message #598744] |
Tue, 22 October 2013 02:39 |
ThomasG
Messages: 3211 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Just did the "explain plan way" out of curiosity, which might be more "multi-user-friendly" than creating a view:
SQL> create or replace function get_sql_dependencies (p_sql in varchar2)
2 return sys.odcivarchar2list
3 pipelined
4 is
5 pragma autonomous_transaction;
6 BEGIN
7 execute immediate 'DELETE FROM plan_table';
8 execute immediate 'explain plan for '||p_sql;
9 COMMIT;
10 for rec in (
11 SELECT DISTINCT OBJECT_OWNER, object_name, OBJECT_TYPE
12 FROM plan_table WHERE object_type = 'TABLE'
13 UNION
14 SELECT DISTINCT table_owner, TABLE_NAME, '(Via Index)'
15 FROM plan_table
16 join all_indexes ON index_name = object_name
17 ) loop
18 pipe row (
19 rec.OBJECT_OWNER||'.'||reC.object_name||' ('||rec.OBJECT_TYPE||')'
20 );
21 end loop;
22 end;
23 /
Function created.
SQL>
SQL>
SQL> SELECT * FROM TABLE(get_sql_dependencies('select * from dual'));
COLUMN_VALUE
--------------------------------------------------------------------------------
SYS.DUAL (TABLE)
[Updated on: Tue, 22 October 2013 02:40] Report message to a moderator
|
|
|