Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Can I retrieve the where clause Oracle generates from the Enter/Execute query buttons

Posted on 2009-05-05
Medium Priority
Last Modified: 2013-12-18
When a user goes into 'Enter Query' mode, enters some form criteria, and then presses 'Execute Query', is there a way to retrieve the where clause Oracle generates against the data block?
Question by:iamlooper
  • 4
  • 3
LVL 40

Expert Comment

ID: 24306431
Oracle doesn't use a where clause against the data block, the where clause is against the objects (the sql execution engine / optimizer) but as far as retrieving the SQL the user submitted, you can find it multiple ways (besides if Oracle forms gives you an option, as I haven't done forms in years).

1) You can use the V$SQLAREA manually to check queries
2) You can use general auditing
3) You can use FGA (Fine Grained Auditing) against the specific tables or schemas or even for specific users

V$SQLAREA is a view that will contain all SQL being run in the instance, and requires no setup. (select sql_text from v$sqlarea) or if the sql is larger than 1k, use sql_fulltext field which is a clob.

FGA requires a bit of setup, but its eaasy and is my preferred route. If you want to go that route, I can provide info on how to set it up with 10g and up.

As to a Form's specific option, I will let a Forms guru add that as I am not sure.
LVL 40

Expert Comment

ID: 24306447
See the bottom of this thread where we provide examples of setting up FGA to audit an object


Author Comment

ID: 24306893
Thanks mrjoltcola. Let me clarify the reason I want to capture the users query condition. Very simply, if the user queries some records out using Enter_Query and Execute_query, I want to be able to re-execute the form at a later time and retrieve the same records.  

I have a text box on the form where a custom where clause can be typed in, and then executed to 'Filter' the form records. I also want to give the user the option to use Oracles built in Query-By-form tools (Enter_Query / Execute_Query) to filter the records. Ideally I would like to populate the custom where clause text box on the form with the results of the Enter_Query. I can then re-execute the same query condition whenever needed.

The ultimate reason I want to be able to store the where condition is because when the user is looking at a record on the form, they may choose to run some updates using server side procedures, which change the current record being displayed. The only way I know of to get the form to reflect the external changes made to the record, is to save the current block record number, re-execute the query, then go back to the saved record number. If the user had used ENTER_QUERY, then re-executing the form retrieves all records with no filter condition, and the record number is no longer valid. I want to re-execute with the same ENTER_QUERY clause used initially.

If there is a way to refresh the current form record being displayed without re-executing the data block, then that would be a useful solution. Although I would still love to capture the Enter_Query condition to populate the custom where clause text box. I investigated using KEY_ENTQRY and KEY_EXEQRY a while back to see if I could intercept what the user had entered so that I could build a where clause up myself, but could'nt seem to get it working. At the time I thought that I was very close, but maybe just missing a key point somewhere.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 40

Expert Comment

ID: 24307507
So the Oracle form interface and the service side procedure are running in different sessions?

Why can't you integrate those procedures into your form? Are they PL/SQL?

Otherwise, there really is no trick to have the form reload changes from a particular record, you have to reissue a query, however, not knowing forms too well, I cant offer more advice, I'm sorry. I will leave this for a Form guru.

Author Comment

ID: 24307976
They are PL/SQL procedures in a server side package. Even if I moved the procedures to the form I would still have the same issue I think. The table behind the form would still get updated, and the form would still need to be requeried.

Once the user presses the execute query button, there must be a way to intercept what the user entered into the form during the enter_query session. I seem to remember when I looked into this a while back (like a year ago), that you could iterate through the form objects looking for text the user entered? This wuold be the best solution for me if it is indeed possible.

Thanks mrjoltcola. Hopefully a form guru will pick this up and there's a 'OMG that's so simple' solution.
LVL 12

Accepted Solution

jwahl earned 2000 total points
ID: 24311677
did you try the forms built-in GET_BLOCK_PROPERTY('BLOCK_NAME', LAST_QUERY) ?

Author Comment

ID: 24315869

That works beautifully!! So simple too. On the KEY_EXEQRY trigger I simply parse out the WHERE clause from the last_query text string.

That you SO MUCH jwahl !!!

  v_last_query      VARCHAR2(2048);
  v_where_start_pos NUMBER(4,0);
  v_order_start_pos NUMBER(4,0);
  v_where_clause    VARCHAR2(1024);
  v_last_query      := get_block_property('booking',last_query);
  v_where_start_pos := instr(v_last_query,'WHERE');
  IF v_where_start_pos = 0
    v_where_clause := null;
    v_where_start_pos := v_where_start_pos + 6;
    v_order_start_pos := instr(v_last_query,'order by');
    IF v_order_start_pos = 0
      v_where_clause := substr(v_last_query,v_where_start_pos);
      v_where_clause := substr(v_last_query,v_where_start_pos,v_order_start_pos - v_where_start_pos);
    END IF;
  where_clause.other_where_clause := v_where_clause;

Open in new window


Author Closing Comment

ID: 31578088
I can't believe I've been stewing over this for a year! That will teach me to go to the exchange sooner. I thank you again !!!!
btw the last line of the code snippet is missing the block colon. Should be..
:where_clause.other_where_clause := v_where_clause;

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question