How do I debug Oracle dynamic sql in sqlplus? - oracle

I have a PL/SQL statement that uses EXECUTE IMMEDIATE to execute a query. However, I'm having difficulty figuring out how to even get the text of the query that's being executed. I can't use dbms_output as the query is greater than 255 characters. Is there any way to make sqlplus echo the string that's passed in to EXECUTE IMMEDIATE?

What version of Oracle are you using? 255 is the default line length for DBMS_OUTPUT.PUT_LINE(). Before 10g we could display 1048 characters in a single call. Since then it is 32K.
Alternatively you should consider using an IDE which supports DBMS_DEBUG. For instance, SQL Developer does so, and it is free from Oracle. Find out more.

You can try to attach a profiler to the database (honestly I have only done that for SqlServer) and run the procedure since the profiler will show any query made to the DB you will be able to pick it up there and do the necessary debugging.
Hope it helps..

How to print large strings N characters at a time.
Modify to suit your needs.
FOR i IN 0..10 LOOP
dbms_output.put_line(substr(my_very_long_string,i*100+1,100));
END LOOP;

You could insert the string into a logging/temporary table and examine it.

Related

dbms_output.put_line not working for type variable

I am working on oracle 11g and am new to the PLSQL part of oracle.
I've trying out a program that has to give the output as 'King' but for some reason the dbms_output.put_line does not seem to work for a type variable. I tried checking it out with a normal user number variable just in case to see if the difference and it seems to be working fine for it.
Thank you for the help.
You need to use:
SET SERVEROUTPUT ON;
Before running the anonymous block to tell the SQL/Plus client that you expect it to output the console.
MT0 gave you the answer: run the SQL Plus command SET SERVEROUTPUT ON before you run the procedure.
You should understand a few things from this.
First, the issue has nothing to do with the data type of your variable; it would apply equally well if you didn't have any variables and the entire body of your procedure was simply to print the hard-coded string 'Hello World'.
Second, the important point that put_line writes the "output" to a memory buffer - not directly to the screen. What you do with the data from the buffer is no longer PL/SQL, it's a function of (a task for) your client software, SQL Plus in your case. SET SERVEROUTPUT ON is a SQL Plus command, which instructs SQL Plus to read the data from the buffer and display it on the screen.
You should use command as below:
set server output on ;
After that you will see output .

Replace characters (&amp) in oracle

I'm trying to use the inbuilt oracle function to replace '&amp' with &. I wrote two functions below but it's not working for me. On running these two in sql developer tool its asking me for input. My requirement is to replace html entities.
select REPLACE('&', '&', '&') from DUAL;
select regexp_replace('&', '&', '&') from DUAL;
Could any one please tell me what's wrong I am doing?.
This should probably be marked as a duplicate, but you need to add this to your script
SET SCAN OFF - that tells us to ignore the & which is used for replacing text when running code in SQLPlus
Once you have that disabled, you can run the queries one at a time with data grids (the first execution mode) or as sqlplus scripts (the second execution mode).
You should execute set define off before executing your query to suppress processing of substitution variables which start by &.
The command set define off will stay active until you enter set define on on your sqlplus session or sql developer worksheet.
thatjeffsmith answer was right but set scan off is tagged obsolete by Oracle. Anyway, it still work.

how to find the query used for creation of Temporary table in Oracle sql developer

I have created a temporary table in oracle sql developer but I forgot to save it and now I want to reuse the query but I don't remember the code used then. Is there a process to get query used creation of temp table?
You can use dbms_metadata.get_ddl()
select dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME_HERE')
from dual;
The result is a CLOB with the complete DDL. You might need to adjust the display in SQL Developer to make the content of that value fully visible (I don't use SQL Developer, so I don't know if that is necessary and if so, what you would need to do)
Edit:
It seems SQL Developer can't display the result of this query properly unless you use the "Run Script" option. And with that you need to use a SET LONG 60000 (or some other big number) before you run it, to see the complete source code:

How to print string using pl/sql without appending the new line character at end

If I use DBMS_OUTPUT.PUT_LINE() method, it always appends a new line character at end.
But what if I want to print lets say Fibonacci series in same line.
I know the logic of fibonacci...
I just want to know which method would I use to put whole output in same line.
I am using PL/SQL
Use DBMS_OUTPUT.PUT instead of DBMS_OUTPUT.PUT_LINE
The entry point for all the doc of version 11gR2 is All Books for Oracle Database Online Documentation Library 11g Release 2 (11.2).
For other Oracle database versions you might want to start your exploration from Database Documentation. Since version 12.1 the URLs are easier : http://docs.oracle.com/database/121/nav/portal_booklist.htm for 12.1 and http://docs.oracle.com/database/122/nav/portal_booklist.htm for 12.2.
You might want to check PL/SQL Packages and Types Reference for that kind of question. In particular the Summary of DBMS_OUTPUT Subprograms. The doc is really handy as a reference when you have questions about the PL/SQL API.

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