Parsing which columns used in a query

Hi,
i try to write my own advisor :-) I´m looking for a package that can help me to find out which columns a used in query in a codition
Example:
select 1 from emp
where deptno=10
and job='CLERK';
=> deptno and clerk
I worked with col_usage$, but it seems not always correct, and it´s not updated when you zuse explain plan ...
Does anybody know, how Oracle´s dbms_advisor/dbms_sqltune finds out which columns are used in a condition ?
Thanks
Marco

mpatzwahl wrote:
i try to write my own advisor :-)Oh dear, that is completely insane. Why not use the one provided?
You would need to write a SQL parser.
The language definition is here
http://docs.oracle.com/cd/E11882_01/server.112/e26088/toc.htm
And don't forget that a function could be used in the condition so you will need to parse PL/SQL also
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/toc.htm
Try it out on something simple like this and see how far you get
{message:id=1908539}
SQL> create or replace function f1 return varchar2 as
  2  l_dummy dual.dummy%type;
  3  begin
  4    select dummy into l_dummy from dual;
  5    return l_dummy;
  6  end;
  7  /
Function created.
SQL> edi
Wrote file afiedt.sql
  1  create or replace function f2 return varchar2 as
  2  begin
  3    return 'X';
  4* end;
SQL> /
Function created.
SQL> create or replace view v as
  2  select
  3      dummy a,
  4      (select dummy from dual) b,
  5      f1 c,
  6      f2 d,
  7      'X' e
  8  from dual;
View created.
SQL> select * from v where c = 'X';
A B C D E
X X X X X

Similar Messages

  • ADF - How to create table which column header come from query result

    I would like to create table that show the summary of task for each month, the query result such as
    Status MonthofYear Total Task
    03 2007/06 9
    03 2007/05 12
    03 2007/04 10
    03 2007/03 7
    05 2007/06 6
    05 2007/05 3
    06 2007/06 1
    09 2007/06 1
    And the table that I would like to show in jdeveloper is
    Status 2007/06 | 2007/05 | 2007/04 | 2007/03 | ......... | 2006/05
    03 9 | 12 | 10 | 0 | .......
    05 6 | 3 | 0 | 0 | .......
    06 1 | 0 | 0 | 0 | .......
    09 1 | 0 | 0 | 0 | .......

    Lucas worked out a solution for this:
    http://technology.amis.nl/blog/?p=2070

  • How to know number of columns used in a cursor

    Hi,
    I am using some query in a cursor.
    I want to know the number of columns used in that query.
    Please let me how to do ?
    This is required because I want to export an oracle report into MS Excel.
    For this I need to know, upto which column I should write in excel cell.
    I am using Oracle 9i
    I hope, I am clear in my requirement.
    Regards
    Surendra

    You could use DBMS_SQL.DESCRIBE_COLUMNS :
    SQL> CREATE OR REPLACE
      2    FUNCTION column_count(
      3                          p_stmt VARCHAR2
      4                         )
      5      RETURN NUMBER
      6      IS
      7          v_cur     NUMBER;
      8          v_cnt     NUMBER;
      9          v_rec_tab DBMS_SQL.DESC_TAB;
    10      BEGIN
    11          v_cur := DBMS_SQL.OPEN_CURSOR;
    12          DBMS_SQL.PARSE(v_cur,p_stmt,DBMS_SQL.NATIVE);
    13          DBMS_SQL.DESCRIBE_COLUMNS(v_cur,v_cnt,v_rec_tab);
    14          DBMS_SQL.CLOSE_CURSOR(v_cur);
    15          RETURN v_cnt;
    16  END;
    17  /
    Function created.
    SQL> SET SERVEROUTPUT ON
    SQL> EXEC DBMS_OUTPUT.PUT_LINE('column_count is ' || column_count('select * from emp'))
    column_count is 8
    PL/SQL procedure successfully completed.
    SQL> EXEC DBMS_OUTPUT.PUT_LINE('column_count is ' || column_count('select min(sal),max(sal) from emp'))
    column_count is 2
    PL/SQL procedure successfully completed.
    SQL> SY.
    Edited by: Solomon Yakobson on Sep 20, 2008 4:54 AM

  • How to use a Formula Column in the main query?

    Hi All,
    I've tried to use a formula columns defined in some query in the condition of that query like this:
    where (:cf_ex - :cf_ex2) >= 5
    but when I run the report no data returned! Why? and how to use it the condition of the query?
    Note: I'm using Forms 6i

    where (:cf_ex - :cf_ex2) >= 5You cannot do that. Formula columns are not part of the select statement (which runs in the database), but are processed in the report.
    When you created this query, my guess is that you got the message "Note: the query Q1 has created the bind parameter(s) cf_ex, cf_ex2". Check these User Parameters in your data model. So, you are actually referencing user parameters in the query, not formula columns.
    I made a computations and things using PL/SQL that can't be done in the select statement in the data model!If it's pl/sql you can probably use that in your query. Create some database functions for cf_ex and cf_ex2 and use these in your query.

  • Improving query which is using IN clause

    Hi,
    I am currently using a query which has IN (1.2.3....N) clause in where condition.
    The table has more than 100000 records in it. I have a set of 20 values which I pass inside this IN clause.
    The column which is used for IN clause is indexed.
    Can I use anything other than IN clause for this purpose?
    What is better way?
    Thanks,
    Plad

    > Can I use anything other than IN clause for this purpose? > What is better way?
    Sure you could use something else, but the IN clause would perfectly suit your needs. And the one who has to maintain the code after you are gone, will be glad you used IN, because it is as clear as you can get.
    If you are hinting at the fact that the query is too slow, then please read [url http://forums.oracle.com/forums/thread.jspa?threadID=501834&tstart=0]this thread and try to find out how many rows out of the 100K satisfy your IN predicate.
    Regards,
    Rob.

  • Input enable query column using Planning function

    Is it possible to input enable a column in query based on user action in Radio button group or button group by calling a planning function or WAD?
    By default the column property for query will be Planning not allowed, but a user action should change the mode to Planning allowed using planning function or entries.
    Thanks

    Hi Ravi,
    you could try to write an exit data slice and a planning function which sets the data slice to not lock the data during runtime. You can find several threads in this forum. how an exit data slice can be used to control the locking of data during runtime.
    Together with the command I mentioned in my last answer this may work, but it still depends on your requirements. A dataslice checks only the combination of characteristics. If you can identiy a characteristic for each column this may work. If not, there is no characteristic combination which can be locked. Please consider this when designing your application. This may lead to an additional characteristic which is used just to identify which column should be locked.
    Hope this helps ...
    Regards Matthias Nutt
    SAP Consulting Switzerland

  • How to use a calculated column in the same query

    Hi All,
    I need some help with using a calculated column in the same query.
    For eq
    I am joining a couple of tables and some of the select columns are calculated based on the columns of the tables and i want a new column in the same query to use this calculated feild in some other calcualtion.
    something like this...
    select (12+3) as Sum1, (12-3) as Sum2, (Sum1 + Sum2 ) as Sum3
    from dual
    or
    select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) THEN 'Yes' ELSE 'No' END
    from dual
    Thanks

    user548171 wrote:
    select (12+3) as Sum1, (12-3) as Sum2, (Sum1 + Sum2 ) as Sum3
    from dual
    or
    select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) THEN 'Yes' ELSE 'No' END
    from dual
    ThanksWhat about just repeating the column values:
    select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( (12+3) / (12-3)  * 100  > 0 )  THEN 'Yes' ELSE 'No'  END FROM DUAL

  • How to retrieve multiple columns using "returning" in the Insert query.

    hi,
    wanted to know how to retrieve multiple columns using "returning" in the Insert Query.
    For retrieving one column we write the query as follows:
    Insert into TABLE values(1,2,3,4) returning COLUMN1 into PARAMETER
    But can we retrive multiple columns in the same query?
    am using oracle 10g and coding in .NET

    Hi,
    You can definetely get multiple values from a single query using the 'returning' clause.
    Eg : insert into emp (empno, ename, job, deptno) values (7324,'ADAM','MARKETING',30) returning ename, deptno into var1, var2; PN : var1 & var2 to be declared as varchar2 & number respectively.
    More insight into the 'RETURNING' clause in this link.
    http://www.samoratech.com/PLSQL/swArtPLSQLReturn.htm
    Regards,
    Bhanu.

  • How to use dynamic select query which queries from 3 different table.

    Hi All,
    I am new to Toplink, i would like to use a named query to select some of the columns from 3 different tables with dynamic where clause.
    I have used the following lines. Please tell me how to get code for the dynamic where clause.
    First try :
    Vector objPersons = (Vector)session.executeQuery("findPersonByGlobalID",Person.class,vQueryArguments);
    The above method is not returning the vector or collection.
    Second Try:
    ReadAllQuery query = new ReadAllQuery(Person.class);
    query.useCollectionClass(LinkedList.class);
    LinkedList person = (LinkedList) session.executeQuery(query);
    The second try is returning the collection but, this fetches all the record from the table.
    1. How to query for range of records?
    2. How to query from multiple tables for some range. How to use dynamic range values ?
    Please reply with your answers or some pointers would help.
    Thanks and Regards,
    Vijay.B

    Hi,
    Did you try using a SQLCall ? It might be able to satisfy you requirements .. :-
    Employee employee = (Employee) session.executeSelectingCall(
        new SQLCall("SELECT * FROM EMPLOYEE WHERE EMP_ID = 44")
    );You can get more information here :-
    http://www.oracle.com/technology/products/ias/toplink/doc/1013/MAIN/_html/qrybas004.htm
    Regards,
    Sandeep

  • Used Columns in SQL script/query

    Hi,
    we really have many SQL scripts.
    Since we expect changes within the schema, we need to know the scripts which have to be reworked.
    Is there any tool available which identifies all columns refereced by a sql script (columns used in select-list, where-clause, order-by clause, ...) ?
    Axel.

    There is a function in TOAD (http://www.toadsoft.com/) Schema Browser you select the table that need to be changed then you select the tab "used by" and it will show the stored procedures that are using this TABLE (not columns). Also this option only display stored code in the database and not unnamed scripts.
    This is a script you can use:
    Select owner, object_type, object_name, object_id, status
    from sys.DBA_OBJECTS where object_id in (
    Select object_id
    from public_dependency
    connect by prior object_id = referenced_object_id
    start with referenced_object_id = (
    Select object_id from sys.DBA_OBJECTS
    where owner = 'OWNER'
    and object_name = 'TABLE_NAME'
    and object_type = 'TABLE' ))
    I hope this is useful to you.
    Message was edited by:
    Delfino N.

  • Hideing a Column in ALV Table which is Displaying the POWL Query data

    Hi SDNers,
    I want to hide a column of ALV Table which is displaying my POWL Query data in it and hideing should be based on the Role as well. i.e few of the role the column shoudl be hidden and few of the roles should have the Column visible.
    waiting for the Valueble Answers.
    Thanks & Regards,
    Govindu

    Hai Kris,
        I took help from ur link and i declared a global attribute request_number.
    and i created an event handler ON_ALV_INSERT and did the follwing coding for giving default value wen ever am inserting new row.
    FIELD-SYMBOLS: <wa_row> LIKE LINE OF r_param->t_inserted_rows.
      DATA bill_details TYPE REF TO zdom_bill_detail.
      LOOP AT r_param->t_inserted_rows ASSIGNING <wa_row>.
        bill_details ?= <wa_row>-r_value.
        IF bill_details->REQ_NUMBER IS INITIAL.
      DATA lo_nd_bill_detail TYPE REF TO if_wd_context_node.
      DATA lo_el_bill_detail TYPE REF TO if_wd_context_element.
      DATA ls_bill_detail TYPE wd_this->Element_bill_detail.
    navigate from <CONTEXT> to <BILL_DETAIL> via lead selection
      lo_nd_bill_detail = wd_context->get_child_node( name = wd_this->wdctx_bill_detail ).
    lo_el_bill_detail = lo_nd_bill_detail->get_element( index = <wa_row>-index ).
          lo_el_bill_detail->set_attribute(
            EXPORTING
              name  = 'REQ_NUMBER'
              value = wd_comp_controller->request_number
    Wen am  setting the value of wd_comp_controller->request_number to my context attribute am getting NULL object ref error.
    lo_el_bill_detail->set_attribute(
        name =  `REQ_NUMBER`
        value = wd_comp_controller->request_number ).
    Pls give some suggestions,
    Thanks in Advance,
    Nalla.B

  • Audit mviews which are used by query rewrite

    Hi all,
    on of our main customers want to find out which MVIEWS are used after an automated query rewrite.
    Example:
    select * from OP_SUM_BACKLOG_DAY_SAP_001;
    select * from dba_audit_trail
    where timestamp > trunc(sysdate)
    and username = 'SMITH'
    and obj_name = 'OP_SUM_BACKLOG_DAY_SAP_001'
    This following statement is using the same MVIEW because of aggregations and the use of query rewrite:
      SELECT fbld_ddat_sales_month,
             SUM (fbld_dummy_zero) fbld_dummy_zero,
             SUM (fbld_dummy_null_number) fbld_dummy_null_number,
             SUM (fbld_oab_quantity) fbld_oab_quantity
        FROM op_fct_backlog_day_sap
    GROUP BY fbld_ddat_sales_month;
    This query is never listed in dba_audit_trail!!!
    Plan
    3 SELECT STATEMENT ALL_ROWS Cost: 291 Bytes: 52 Cardinality: 2 CPU Cost: 0 IO Cost: 0 Time: 00:00:00 Partition #: 0
    2 HASH GROUP BY Projection: (#keys=1) "OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_DDAT_SALES_MONTH"[VARCHAR2,6], SUM("OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_OAB_QUANTITY")[22], SUM("OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_DUMMY_NULL_NUMBER")[22], SUM("OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_DUMMY_ZERO")[22] Cost: 291 Bytes: 52 Cardinality: 2 CPU Cost: 0 IO Cost: 0 Time: 00:00:00 Partition #: 0
    1 MAT_VIEW REWRITE ACCESS FULL DWHOP.OP_SUM_BACKLOG_DAY_SAP_001 [Analyzed] Projection: "OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_DDAT_SALES_MONTH"[VARCHAR2,6], "OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_DUMMY_ZERO"[NUMBER,22], "OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_DUMMY_NULL_NUMBER"[NUMBER,22], "OP_SUM_BACKLOG_DAY_SAP_001"."FBLD_OAB_QUANTITY"[NUMBER,22] Cost: 290 Bytes: 1.016.600 Cardinality: 39.100 CPU Cost: 0 IO Cost: 0 Time: 00:00:00 Partition #: 0
    Any ideas how to audit this access???
    Regards
    Stephan

    Hi,
    Get the UID of your query from the query designer (select query in the properties window, goto Advanced tab).
    Pass it to table RSZELTXREF (pass query UID to field SELTUID and 'VAR' to field LAYTP). This gives the list of variables used in the query. Now got to table RSZGLOBV, pass all the variable IDs (field values for TELTUID from table RSZELTXREF) to the field VARUNIID and restrict VPROCTP to '6'. This gives a list of all the auth variables used in a query.
    Regards,
    Murali.

  • How to accessing current row report column value in Lov Query?

    Hi,
    which access methods (eg. bind variables, substitutions, ...) for getting the current row report column value can be used in the "Lov Query" property of a report column?
    As what I know of and what I have read on the forum there are no bind variables for the report columns. For the "Link Text" property it seems that the column values exist as substitution strings (#COLUMN_NAME#). But they don't work in the Lov Query. => And would be good because of a hard parse each time the Lov query is executed.
    The following post (Re: Simulating a correlated sub query in lov
    is showing a solution to use package variables for temporary storage of the referenced value, but the only problem with that solution is that if a new record is added with the "Add rows to tabular form" process the package variable still contains the value from the last queried row! Is there a way (variable, APEX package, ...) to determine if the lov query is executed for a new record so that the package can return null?
    I know that I could write the package in a way that the value is immediately cleared when lov_pkg.keyval is called (one time read), but then I would have to create several variables if I'm accessing the value multiple times in the query or in another query => I think an one time read solution would be very obscurely.
    Thanks for your help
    Patrick
    http://inside-apex.blogspot.com

    Hi Patrick,
    I agree that it's a waste to continually use Ajax to go back to the server to get the contents of a dynamic select list.
    There are no bind variables for any row item - but what you do have, as per my previous post, is the value of the data entered by the user in the first row. You can pass this into your application process (using get.add("VARIABLENAME", value)), which can use it to retrieve the correct LOV in your Ajax code - this will give you a "bind variable" that your process can use.
    What you could do, however, is generate hidden select lists on your page - one for each possible LOV list and replace the contents of the new row's select list with the contents of the appropriate hidden select list. This is easy to do with javascript (using innerHTML functions). Obviously, though, the usefulness of this depends on the number and size of the select lists.
    Even if you don't generate them to start with, you can keep a copy of any select lists returned by Ajax in the DOM for use on new rows. So, if you have retrieved a select list, you will have a copy of it in DOM which you can then copy into the new row. If you don't have the list in DOM, use Ajax to get it, store a copy of it and copy it into the new row.
    Which method you use will depend on the number/size of select lists needed. If they are few in number and/or size, I would suggest generating hidden lists. If they are large, use Ajax to get them once, store them and then retrieve them from the DOM when needed.
    There is another thread here where Arie recommends going to the server every time to make sure you get the most up-to-date data for the lists. If you want to follow this advice, for this reason, use get.add("VARIABLENAME", value) to pass the value to your process. If this is not an issue, you can use one of the other methods I outlined above.
    Regards
    Andy

  • Returning operation, table and column from a sql query

    Hi,
    I am working on Oracle 10g and i have to do achieve something like this.
    i have to create procedure or functions which will accept a SQL query and will return following :
    1).  Operation like 'SELECT', 'INSERT', 'UPDATE', 'DELETE' etc.
    2).  Column names
    3).  Table involved.
    One way to achieve this would be through hard parsing of string which might be time consuming. So do we have any in-built oracle package or function for the same job ?
    Regards,
    Vikas Kumar

    Check for DBMS_SQL package and DESCRIBE_COLUMNS procedure in that package.
    You might need a combination of this package and Hard parsing of the input string.
    For operation you can trim any space and brackets using TRIM function and then you can extrcat first 6 characters using SUBSTR for Operation.
    I have not worked or used DBMS_SQL so wont be able to help you much on this.
    Here is the link for DBMS_SQL
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm#i1026120
    Regards
    Arun

  • How to make an index use in a query execution

    Hi,
    I have the below query for which ename column has an index. As of my knowledge below queries 1st and 2st will not use index. Hence i used the 3rd statement and that too its not using the index. Finally i used the 4th query, but even the 4th query is not using the index. Then how do i make this query to use my index??? Do i need to create a function based index for this??? Is that the final option????
    1. select * from emp where ename !='BH' ;
    2. select * from emp where ename <> 'BH';
    3. select * from emp where ename not in ('BH');
    4. select * from emp where ename < 'BH' or ename > 'BH';
    Regards,
    007
    Edited by: 007 on Jun 6, 2013 7:56 AM
    Edited by: 007 on Jun 6, 2013 8:06 AM
    Edited by: 007 on Jun 6, 2013 8:06 AM
    Edited by: 007 on Jun 6, 2013 8:06 AM
    Edited by: 007 on Jun 6, 2013 8:12 AM

    Sorry 007, I really thought you were posting a trick question as on the OCP tests.
    Anyway, as Justin mentioned, if you have an index on ename, it may be used when doing a comparison predicate statement with the ename value.
    What it depends on are several other things: stats, how many rows in the table, use of an index hint, etc.
    Rather than questioning the group on this, why not just turn on autotrace and run the query for the different scenarios.
    The output will show you if it used the index, number of rows returned, blocks read, etc.
    SQL> create table emp (ename  varchar2(40));
    Table created.
    SQL> insert into emp select username from sys.dba_users;
    25 rows created.
    SQL> commit;
    Commit complete.
    SQL> set autotrace on
    SQL> select * from emp where ename != 'SYSTEM';
    Execution Plan
    Plan hash value: 2951343571
    | Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT |           |    24 |   528 |     1   (0)| 00:00:01 |
    |*  1 |  INDEX FULL SCAN | ENAME_IDX |    24 |   528 |     1   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("ENAME"<>'SYSTEM')As you can see, the above used an index, even though there were only 25 rows in the table.
    You can test each of your scenarios, one by one, including use of a hint.

Maybe you are looking for

  • How can I sort multiple tables on a single page as if they were one continuous table?

    I have a single narrow column of numbers that cover multiple pages. I would like to do either of the following: break that long single column into multiple colums that fit on one page and are still able to be sorted in that arrangement OR sort the lo

  • Re: Protecting your system

    Macs are just as vulnerable to SPAM and viruses as PC's, don't kid yourself. I write programs, IT Tech and security is my specialty. Your Mac is not only likely to have SPAM, I'd bet on it. There is only one way to completely safeguard your system, o

  • Getting a Mail session from Initialcontext

    Hi, Does anyone know how to configure oc4j such that i could get a mail session by looking up in the initial context? I know it could be done in jboss by altering an xml file. But I am not sure how to do it in oc4j. Could someone help please? Could a

  • Table for Owners Data

    Hello Experts, Hi have been given an assignment to find out Query's and Web Template,s Owner and Last changed by Data, since there are more than 200 queries, is there any Table which help me out giving these infos. thanks Sulochan

  • Can't apply episode retention settings to podcasts

    My large number of podcasts are set to check for (and download) new episodes every day, and to keep all episodes.  Unfortunately, I rarely have time to listen to them.  In my iTunes app, a large number of episodes have accumulated under each podcast.