Alternative to dbms_output.putline - oracle

I am creating a dynamic query in a procedure and now want to see it through dbms_output.putline, but my query contains more than 255 characters.
How to view the query?
What are the alternates of dbms_output.putline?

There's a little bit of confusion going on.
In Oracle 9i dbms_output.put_line is limited to 255 characters. This restriction was removed in 10g and is similarly not present in Oracle 11g.
You have tagged your question oracle10g, which means that you're limited to 32,767 bytes, the standard PL/SQL maximum.

try mess around something like
create or replace procedure custom_output(in_string in varchar2 )
is
out_string_in long default in_string;
str_len number;
loop_count number default 0;
begin
str_len := length(out_string_in);
while loop_count < str_len
loop
dbms_output.put_line( substr( out_string_in, loop_count +1, 255 ) );
loop_count := loop_count +255;
end loop;
end;
/

Related

ORA-06502 with ORA-06512

I have a procedure in which I'm trying to write a source code (1290 lines) to dbms_output like this:
dbms_output.put_line(DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner')); --MYPROC, line 6
I'm getting :
ORA-06502: PL/SQL: numeric or value error
ORA-06512: in "MYPROC", line 6
.
This error occures in toad.
I can execute in editor tab of toad:
SELECT DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner') FROM DUAL;
I mean I'm getting the source code in 'Data grid'.
Same happens when I try to store the code in a CLOB variable:
src CLOB;
...
src := DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner') ; --MYPROC, line 6
Any clue?
From the documentation for dbms_output:
The maximum line size is 32767 bytes.
That means that you can't pass more than that in a single put_line call. You are currently passing your whole CLOB, which at 1290 lines is likely to exceed that limit. And the error you get when you do that is "ORA-06502: PL/SQL: numeric or value error", as you are seeing.
You can split your CLOB into smaller chunks, and as it is already multiple lines it makes sense to make each chunk a single line from the DDL. You can do that by looking for newline characters, extracting all the text up to the next one, and printing that. You need a few variables to keep track of where you are. Something like this should work for you:
declare
src clob;
src_length pls_integer;
pos pls_integer := 1;
buffer varchar2(32767);
amount pls_integer := 32767;
begin
src := dbms_metadata.get_ddl('FUNCTION', 'TEST_FUNCTION_1', user);
src_length := dbms_lob.getlength(src);
while pos < src_length loop
-- read to next newline if there is one, rest of CLOB if not
if dbms_lob.instr(src, chr(10), pos) > 0 then
-- see how many charcaters there are until next newline
amount := dbms_lob.instr(src, chr(10), pos) - pos;
-- if there are any, read them into the buffer; otherwise clear it
if amount > 0 then
dbms_lob.read(src, amount, pos, buffer);
else
buffer := null;
end if;
pos := pos + amount + 1; -- skip newline character
else
-- no newline so read everything that is left
amount := 32767;
dbms_lob.read(src, amount, pos, buffer);
pos := pos + amount;
end if;
dbms_output.put_line(buffer);
end loop;
end;
/
It won't work if you have a single line (without or without a newline at the end) that is more than 32k, which hopefully won't be an issue with DDL. (You could sort of handle it, but doing so would inject additional newlines, which wouldn't be good either.)
What you are saying can't be true. DBMS_OUTPUT.PUT_LINE can't be used at SQL level, it belongs to PL/SQL.
What is MYPROC and what does it contain at line #6?
Which "editor" is "I can execute in editor"?
Numeric or value error is usually related to the fact that you're trying to store "large" values into a "small" variable:
SQL> declare
2 l_var varchar2(2);
3 begin
4 l_var := 'ABC';
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
SQL>
which is what - I presume - you did.
Another cause is wrongly declared variable, e.g.
SQL> declare
2 l_var number;
3 begin
4 l_var := 'A';
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4
SQL>
I'll try to guess what you might be doing:
SQL> set serveroutput on
SQL> DECLARE
2 src CLOB;
3 BEGIN
4 src := DBMS_METADATA.GET_DDL ('PACKAGE', 'MY_PKG', 'SCOTT');
5 DBMS_OUTPUT.put_line ('len = ' || DBMS_LOB.getlength (src));
6 END;
7 /
len = 67239
PL/SQL procedure successfully completed.
SQL>
As you can see, it works OK for me. Package isn't that small (see its length), so - can't really tell what you did wrong. I'd suggest you to do exactly as I did - copy/paste code I posted above (those 7 lines), fix information (function, its name, owner) and post the result by editing the original question, not as a comment.

Finding the exponent (without using POW or built in functions) of a number using a procedure and/or Function

I am trying to create a way to find the exponent of a number (in this case the base is 4 and the exponent 2 so the answer should be 16) using a procedure without using the POW Function or any built in functions to find the exponent. Eventually I would like to take input numbers from the user.
set serveroutput on;
CREATE OR REPLACE PROCEDURE Exponent(base number, exponent number) as
answer number;
BEGIN
base := 4;
exponent := 2;
LOOP
IF exponent > 1 THEN
answer := base * base;
END IF;
END LOOP;
dbms_output.put_line('Answer is: ' || answer);
END;
/
Error(7,25): PLS-00103: "expression 'BASE' cannot be used as an assignment target" and "expression 'EXPONENT' cannot be used as an assignment target"
Any ideas on how to solve the error and/or better ways of getting the exponent without using built-in functions like POW?
In your procedure base and exponent are input parameters and can't be changed. You've got a couple of options:
1) copy the parameters to variables internal to the procedure and manipulate those internal values, or
2) change the parameters to be input/output parameters so you can change them.
Examples:
1)
CREATE OR REPLACE PROCEDURE Exponent(pin_base number, pin_exponent number) as
base number := pin_base;
exponent number := pin_exponent;
answer number;
BEGIN
base := 4;
exponent := 2;
LOOP
IF exponent > 1 THEN
answer := base * base;
END IF;
END LOOP;
dbms_output.put_line('Answer is: ' || answer);
END;
2)
CREATE OR REPLACE PROCEDURE Exponent(base IN OUT number,
exponent IN OUT number) as
answer number;
BEGIN
base := 4;
exponent := 2;
LOOP
IF exponent > 1 THEN
answer := base * base;
END IF;
END LOOP;
dbms_output.put_line('Answer is: ' || answer);
END;
The best thing is that whatever Oracle provides as inbuilt functionality that serves the purpose in a best possible. (Almost all the times better then customized codes) Try to use EXP function. I have tried to make customized code per my understanding. Hope this helps.
CREATE OR REPLACE
FUNCTION EXP_DUMMY(
BASE_IN IN NUMBER,
EXPO_IN IN NUMBER)
RETURN PLS_INTEGER
AS
lv PLS_INTEGER:=1;
BEGIN
FOR I IN
(SELECT base_in COL1 FROM DUAL CONNECT BY level < expo_in+1
)
LOOP
lv:=lv*i.col1;
END LOOP;
RETURN
CASE
WHEN EXPO_IN = 0 THEN
1
ELSE
lv
END;
END;
SELECT EXP_DUMMY(2,4) FROM DUAL;

How to get full value of clob while debugging PL SQL script on sql developer

I'm going in debug in a procedure PL/SQL on SQL Developer that make a big query and store it in a CLOB. When i take the value of that CLOB, the value is truncated. Can anyone know any method to take a full value of CLOB in debug mode?
This procedure should help you, if I understood correctly you want to print the full value of clob?
PROCEDURE PRINT_CLOB(par_clob IN CLOB) IS
ln_offset NUMBER DEFAULT 1;
BEGIN
LOOP
EXIT WHEN ln_offset > dbms_lob.getlength(par_clob);
dbms_output.put_line(dbms_lob.substr(par_clob, 255, ln_offset));
ln_offset := ln_offset + 255;
END LOOP;
END PRINT_CLOB;
May be this might not be the answer you are looking for but in my application I have created a dummy table with column data type as clob. So whenever I want to get the value I will insert into this temporary table.
Thanks to #gregorianisch answer, I made my own version, which simply takes into account a carriage return to calculate the next chunk, to not cut any message (I needed to print query statements)
PROCEDURE print_clob(
input_string IN CLOB
)
IS
out_string_in CLOB default input_string;
clob_length NUMBER := DBMS_LOB.GETLENGTH(out_string_in);
v_offset NUMBER default 0;
v_chunk_size NUMBER := 32767;
v_substr CLOB;
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
WHILE v_offset < clob_length
LOOP
v_chunk_size := 32767;
v_substr := SUBSTR(out_string_in, v_offset + 1, v_chunk_size);
v_chunk_size := CASE WHEN INSTR(v_substr, CHR(10), -1) > 0 THEN INSTR(v_substr, CHR(10), -1) ELSE 32767 END;
v_substr := SUBSTR(out_string_in, v_offset + 1, v_chunk_size);
DBMS_OUTPUT.PUT_LINE(v_substr);
v_offset := v_offset + v_chunk_size;
END LOOP;
END print_clob;

Oracle - ORA-06502: PL/SQL: numeric or value error (DBMS_OUTPUT)

I implemented a function that returns clob data-type, and I would like to print the result in DBMS Output. Unfortunately, I am getting ORA-06502: PL/SQL: numeric or value error and I think it is due to the size of DBMS_OUTPUT.
This is the code.
DECLARE
TYPE tp_col_array IS TABLE OF varchar2(32767);
FUNCTION my_fn (
p_in_proc_date IN varchar2)
RETURN clob AS
vr_output_str clob;
BEGIN
-- Detailed code hidden due to privacy. Sorry
RETURN vr_output_str;
EXCEPTION
WHEN LOGIN_DENIED
THEN
DBMS_OUTPUT.PUT_LINE('Invalid username/password: logon denied');
RETURN 'TEST Terminated';
END my_fn;
BEGIN
DBMS_OUTPUT.PUT_LINE(my_fn('31-AUG-14'));
END;
Here are something that can help you to understand this issue
1) Added the following to set the size of buffer unlimited, but did not work..
DBMS_OUTPUT.ENABLE(NULL);
or
set serveroutput on size unlimited;
Related link: http://www.oracle-developer.net/display.php?id=327
2) It is noted that the size of vr_output_str is 75387, and that is why the return type is CLOB.
DBMS_LOB.getlength(vr_output_str); // =75387
3) I can solve the issue by doing the following, but I think this is not a good solution since it executed the function three times.
DBMS_OUTPUT.PUT_LINE(SUBSTR(my_fn ('31-AUG-14'), 1, 32767));
DBMS_OUTPUT.PUT_LINE(SUBSTR(my_fn ('31-AUG-14'), 32768, 32767));
DBMS_OUTPUT.PUT_LINE(SUBSTR(my_fn ('31-AUG-14'), 65536, 32767));
4) I am using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
You will not be able to print a clob using dbms_output.put_line directly if it is greater than 32767 bytes.
If this is the case you can create a procedure to iterate through the clob and print out one smaller chunk at a time. Such a procedure and test script is below:
declare
c clob;
procedure print_clob( p_clob in clob ) is
v_offset number default 1;
v_chunk_size number := 10000;
begin
loop
exit when v_offset > dbms_lob.getlength(p_clob);
dbms_output.put_line( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );
v_offset := v_offset + v_chunk_size;
end loop;
end print_clob;
begin
for i in 1..10000 loop
c := c || 'test';
end loop;
--This will result in ora-06502
--dbms_output.put_line(c);
print_clob(c);
end;
Note that v_chunk_size must result in less than 32767 bytes being chunked at-a-time. If you encoding has 2 bytes per char you will need to use (32767/2).
The following procedure will better:
Oracle 10g has limitation on put_line (a maximum of 32767 characters), But Oracle before 10g has a maximum of 255 characters limitation.
the 'put_line' adding end of line on every iteration loop during output clob. So we use put() better (and 'DBMS_OUTPUT.NEW_LINE' at end).
PROCEDURE print_clob( p_clob in clob ) IS
v_offset number default 1;
v_chunk_size number := 255;
BEGIN
LOOP
EXIT when v_offset > dbms_lob.getlength(p_clob);
dbms_output.put( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );
v_offset := v_offset + v_chunk_size;
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END print_clob;

What's the difference between pls_integer and binary_integer?

I've inherited some code which is going to be the base for some additional work. Looking at the stored procs, I see quite a lot of associative-arrays.
Some of these are indexed by binary_integers, some by pls_integers. Are there any differences between the two?
I had a look at the documentation, but apart from this line:
The PL/SQL data types PLS_INTEGER and BINARY_INTEGER are identical. For simplicity, this document uses PLS_INTEGER to mean both PLS_INTEGER and BINARY_INTEGER.
I couldn't find any difference between the two. So what's the difference? Are both around for historical/compatibility reasons?
I'm using Oracle 10gR2
Historical reasons. They used to be different before 10g:
On 8i and 9i, PLS_INTEGER was noticeably faster than BINARY_INTEGER.
When it comes to declaring and manipulating integers, Oracle offers lots of options, including:
INTEGER - defined in the STANDARD package as a subtype of NUMBER, this datatype is implemented in a completely platform-independent fashion, which means that anything you do with NUMBER or INTEGER variables should work the same regardless of the hardware on which the database is installed.
BINARY_INTEGER - defined in the STANDARD package as a subtype of INTEGER. Variables declared as BINARY_INTEGER can be assigned values between -231+1 .. 231-1, aka -2,147,483,647 to 2,147,483,647. Prior to Oracle9i Database Release 2, BINARY_INTEGER was the only indexing datatype allowed for associative arrays (aka, index-by tables), as in:
TYPE my_array_t IS TABLE OF VARCHAR2(100)
INDEX BY BINARY_INTEGER
PLS_INTEGER - defined in the STANDARD package as a subtype of BINARY_INTEGER. Variables declared as PLS_INTEGER can be assigned values between -231+1 .. 231-1, aka -2,147,483,647 to 2,147,483,647. PLS_INTEGER operations use machine arithmetic, so they are generally faster than NUMBER and INTEGER operations. Also, prior to Oracle Database 10g, they are faster than BINARY_INTEGER. In Oracle Database 10g, however, BINARY_INTEGER and PLS_INTEGER are now identical and can be used interchangeably.
binary_integer and pls_integer both are same. Both are PL/SQL datatypes with range -2,147,648,467 to 2,147,648,467.
Compared to integer and binary_integer pls_integer very fast in excution. Because pls_intger operates on machine arithmetic and binary_integer operes on library arithmetic.
pls_integer comes from oracle10g.
binary_integer allows indexing integer for assocative arrays prior to oracle9i.
Clear example:
SET TIMING ON
declare
num integer := 0;
incr integer := 1;
limit integer := 100000000;
begin
while num < limit loop
num := num + incr;
end loop;
end;
PL/SQL procedure successfully completed.
Elapsed: 00:00:20.23
ex:2
declare
num binary_integer := 0;
incr binary_integer := 1;
limit binary_integer := 100000000;
begin
while num < limit loop
num := num + incr;
end loop;
end;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:05.81
ex:3
declare
num pls_integer := 0;
incr pls_integer := 1;
limit pls_integer := 100000000;
begin
while num < limit loop
num := num + incr;
end loop;
end;
/
Another difference between pls_integer and binary_integer is that when calculations involving a pls_integer overflow the PL/SQL engine will raise a run time exception. But, calculations involving a binary_integer will not raise an exception even if there is an overflow.

Resources