Oracle - Log the executed query - oracle

I have to debug stored procedure which contains a few SQL queries. One of them contains an error. So, I need to execute this SQL query with parameters in other window. I found next query that would help me:
select v.SQL_TEXT
from v$sql v
Unfortunately, this field restricted by 1Kb. In my case I have quite big SQL query and Oracle truncates it. How to log the executed query? I use PL/SQL Developer 10 and Oracle 9i

Unfortunately, this field restricted by 1Kb
If you need the full SQL, then use the SQL_FULLTEXT which is a CLOB datatype instead of SQL_TEXT whcih is limited to first 1000 characters.
From documentation,
Column Datatype Description
------ -------------- ---------------------------------------
SQL_TEXT VARCHAR2(1000) First thousand characters of the SQL
text for the current cursor
SQL_FULLTEXT CLOB Full text for the SQL statement exposed
as a CLOB column. The full text of a SQL
statement can be retrieved using this
column instead of joining with the
V$SQL_TEXT dynamic performance view.
So, use:
SELECT SQL_FULLTEXT FROM v$sql;
By the way, seems like you are actually looking for tracing your session to get the complete details of the procedure and the SQL statements involved. I would suggest to trace the session with level 4 i.e. with the addition of bind variable values.
See How to generate trace file – SQL Trace and TKPROF in Oracle

Related

List all procedures along with the tables and columns used in that procedure in Oracle

Last night got a call from my team lead and he asked me to make the list of all the procedures along with the tables and columns used in Oracle.
I got a query to list all the procedures along with tables and dblink but couldn't get column names (along with DML if possible) used in that Procedure :
select DISTINCT OWNER, NAME, referenced_name, referenced_link_name, referenced_type
from dba_dependencies
where OWNER = 'OWNER_NAME';
My required output is as follows:
Owner_Name
Procedure_Name
Referenced_name
Referenced_link
Referenced_type
Column_Name,
dml_type(select/insert/update).
Please help if possible..
columns will be in DBA_TAB_COLUMNS.
as for the SQL statements, this is a bit more difficult, but doable if you have Diagnostics pack licensed; here's the outline (works in 11g or above):
DBA_HIST_ACTIVE_SESS_HISTORY.top_level_sql_id is the SQL id of the calling procedure,
so listing DBA_HIST_ACTIVE_SESS_HISTORY.sql_id for that top_level_sql_id is "all" the SQL from the execution of the procedure.
DBA_HIST_SQL_TEXT can be queried to get the sql text for the sql_id.
If you don't have the Diagnostics Pack, you'd have to instrument v$session for the top_level_sql_id's and sql_id's and the look in v$sql for the text.

Dynamically logging PLSql statements in a table

I have bunch of plsql which has dynamic queries ( ie queries are framed as string and executed with immediate execute function). I want to find the dependent tables and columns for a plsql. I am planning to achieve by GSQL parser. I tried the plsql file as it is, because of dynamic queries, I am not able to get the dependency information. The alternate way is collecting the list of SQL statements executed during the plsql run. How to get sql statement for a plsql and store it a table with unit name mapping ?
Hi you need execute the sql you have saved with execute immediate;
you will get all sql in below oracle view
select * from v$sql;
select * from dba_hist_sqltext;

Execution of Oracle Procedure prior to Source Qualifier that must use the same Oracle session

I have a security procedure that has to be called just before a select from source table. Without this procedure no rows are returned.
The problem is that I have checked several ways to call this procedure just before Source Qualifier:
Pre-sql into the Source Qualifier As a Stored procedure
Pre-load source
Put several sql sentences in the sql query propertie in Source Qualifier (added 2014-11-08)
Always seems that Powercenter opens a new oracle connection, where security procedure takes no effect.
What could be the correct way to put both process together and use the same Oracle session?
Note added 2014-11-08:
I have tried also to put 2 sentences in the SQL Query of the Source Qualifier:
call procedure('param');
SELECT field1, field2, field.... from VI_ETL...;
and I get error ORA-24333 Zero Interaction Count, perharps because first item is not a SELECT statement that returns rows?
Try using SQL Query on Source Qualifier to invoke a series of statements - the security Stored Procedure being the first one.

view the sql_fulltext[clob] using v$sql?

I'm using select sql_text from v$sql to view the currently running query in my aplication. It shows only 4000 chars of running query. So that I used the following,
select sql_fulltext from v$sql
But it showing only as clob. How can I view the whole query in this case in oracle?
sqlplus has not problems showing CLOB contents, just make sure to ..
set long 2000000
so some such high number > the max size of your CLOB, before running the query.

Explain Plan for Query in a Stored Procedure

I have a stored procedure that consists of a single select query used to insert into another table based on some minor math that is done to the arguments in the procedure. Can I generate the plan used for this query by referencing the procedure somehow, or do I have to copy and paste the query and create bind variables for the input parameters?
Use SQL Trace and TKPROF. For example, open SQL*Plus, and then issue the following code:-
alter session set tracefile_identifier = 'something-unique'
alter session set sql_trace = true;
alter session set events '10046 trace name context forever, level 8';
select 'right-before-my-sp' from dual;
exec your_stored_procedure
alter session set sql_trace = false;
Once this has been done, go look in your database's UDUMP directory for a TRC file with "something-unique" in the filename. Format this TRC file with TKPROF, and then open the formatted file and search for the string "right-before-my-sp". The SQL command issued by your stored procedure should be shortly after this section, and immediately under that SQL statement will be the plan for the SQL statement.
Edit: For the purposes of full disclosure, I should thank all those who gave me answers on this thread last week that helped me learn how to do this.
From what I understand, this was done on purpose. The idea is that individual queries within the procedure are considered separately by the optimizer, so EXPLAIN PLAN doesn't make sense against a stored proc, which could contain multiple queries/statements.
The current answer is NO, you can't run it against a proc, and you must run it against the individual statements themselves. Tricky when you have variables and calculations, but that's the way it is.
Many tools, such as Toad or SQL Developer, will prompt you for the bind variable values when you execute an explain plan. You would have to do so manually in SQL*Plus or other tools.
You could also turn on SQL tracing and execute the stored procedure, then retrieve the explain plan from the trace file.
Be careful that you do not just retrieve the explain plan for the SELECT statement. The presence of the INSERT clause can change the optimizer goal from first rows to all rows.

Resources