Showing posts with label set serveroutput on. Show all posts
Showing posts with label set serveroutput on. Show all posts

Friday, July 13, 2012

How to See the Value of a PL/SQL Bind Variable

You can see the value of a PL/SQL bind variable like this:

Add the following line to your database’s parameter file:
event='10046 trace name context forever, level 4'

Bounce the database.

Run some PL/SQL which uses a bind variable:

SQL> set serveroutput on
SQL> variable bind_variable varchar2(3)
SQL> exec :bind_variable := 'SYS'

PL/SQL procedure successfully completed.

SQL> declare
  2   counter number;
  3  begin
  4   select count(*) into counter
  5    from dba_tables
  6    where owner = :bind_variable;
  7   dbms_output.put_line('Tables owned by SYS = '||counter);
  8  end;
  9  /
Tables owned by SYS = 322

PL/SQL procedure successfully completed.

SQL>

Look at the appropriate lines in the trace file to see the value of the bind variable you have just used (click on your browser's zoom button if necessary as the text is quite small):

=====================
PARSING IN CURSOR #1 len=173 dep=0 uid=22 oct=47 lid=22 tim=46193203462144 hv=1143714290 ad='2b36288'
declare
counter number;
begin
select count(*) into counter
  from dba_tables
  where owner = :bind_variable;
dbms_output.put_line('Tables owned by SYS = '||counter);
end;
END OF STMT
PARSE #1:c=0,e=1024,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=2,tim=46193203462144
BINDS #1:
bind 0: dty=1 mxl=32(03) mal=00 scl=00 pre=00 oacflg=03 oacfl2=0 size=32 offset=0
   bfp=1404b4520 bln=32 avl=03 flg=05
   value="SYS"
=====================
PARSING IN CURSOR #2 len=50 dep=1 uid=22 oct=3 lid=22 tim=46193203463168 hv=2267380128 ad='2ba90d0'
SELECT COUNT(*) FROM DBA_TABLES WHERE OWNER = :B1
END OF STMT
PARSE #2:c=0,e=1024,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=2,tim=46193203463168
BINDS #2:
bind 0: dty=1 mxl=32(03) mal=00 scl=00 pre=00 oacflg=13 oacfl2=1 size=32 offset=0
   bfp=1404b48a0 bln=32 avl=03 flg=05
   value="SYS"
EXEC #2:c=0,e=0,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=2,tim=46193203463168
FETCH #2:c=50000,e=60416,p=0,cr=702,cu=0,mis=0,r=1,dep=1,og=2,tim=46193203523584
EXEC #1:c=50000,e=61440,p=0,cr=702,cu=0,mis=0,r=1,dep=0,og=2,tim=46193203523584
=====================

Monday, July 02, 2012

SET ECHO ON and SET SERVEROUTPUT ON


This was tested on Oracle 9. When you login to SQL*Plus, the default for ECHO and SERVEROUTPUT is OFF. If you SET them both to ON and reconnect to SQL*Plus, ECHO stays ON but SERVEROUTPUT reverts to OFF:

ORACLE9 > sqlplus /

SQL*Plus: Release 9.2.0.7.0 - Production on Mon Jul 2 17:48:08 2012

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Connected to:
Oracle9i Enterprise Edition Release 9.2.0.7.0 - Production
With the Partitioning option
JServer Release 9.2.0.7.0 - Production

SQL> show echo
echo OFF
SQL> show serveroutput
serveroutput OFF
SQL> set echo on
SQL> set serveroutput on
SQL> show echo
echo ON
SQL> show serveroutput
serveroutput ON size 2000 format WORD_WRAPPED
SQL> conn /
Connected.
SQL> show echo
echo ON
SQL> show serveroutput
serveroutput OFF
SQL>

Wednesday, June 27, 2012

%TYPE


This example was tested on Oracle 11.2. It shows how you can use %TYPE to create a variable like another variable. In this example, a variable called MAX_OBJ_ID is defined like OBJECT_ID from DBA_OBJECTS. Then SELECT INTO is used to select the maximum value of OBJECT_ID into it and the result is displayed:

SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2   MAX_OBJ_ID DBA_OBJECTS.OBJECT_ID%TYPE;
  3  BEGIN
  4   SELECT MAX(OBJECT_ID)
  5    INTO MAX_OBJ_ID
  6    FROM DBA_OBJECTS;
  7   DBMS_OUTPUT.PUT_LINE('MAX_OBJ_ID = '||MAX_OBJ_ID);
  8  END;
  9  /
MAX_OBJ_ID = 132369

PL/SQL procedure successfully completed.

SQL>

Finally, the same result is obtained in SQL*Plus:

SQL> SELECT MAX(OBJECT_ID) FROM DBA_OBJECTS
  2  /

MAX(OBJECT_ID)
--------------
        132369

SQL>

Tuesday, March 06, 2012

SERVEROUTPUT

Set serveroutput off/on can be used in SQL*Plus to control the output from the PL/SQL dbms_output.put_line statement. When you first login the default is for serveroutput to be off:

SQL> show serveroutput
serveroutput OFF
SQL>

If serveroutput is set to off, you will not get any output from dbms_output.put_line:

SQL> begin
  2  dbms_output.put_line('12345678901234567890');
  3  end;
  4  /
 
PL/SQL procedure successfully completed.
 
SQL>

Setting it to on changes this:

SQL> set serveroutput on
SQL> show serveroutput
serveroutput ON size 2000 format WORD_WRAPPED
SQL> begin
  2  dbms_output.put_line('12345678901234567890');
  3  end;
  4  /
12345678901234567890
 
PL/SQL procedure successfully completed.
 
SQL>

There is also a size associated with serveroutput. This specifies the volume of output allowed. In Oracle 9 this can have any value between 2000 and 1000000:

SQL> set serveroutput on size 1999
SP2-0547: size option 1999 out of range (2000 through 1000000)
SQL> set serveroutput on size 1000001
SP2-0547: size option 1000001 out of range (2000 through 1000000)
SQL>

I will set it to 2000 for the example below although, as you can see above, this appears to be the default. When you reach the limit set by the size parameter, Oracle displays an error and stops:

SQL> set serveroutput on size 2000
SQL> declare
  2  a number;
  3  begin
  4  a := 1;
  5  while a < 100
  6  loop
  7  dbms_output.put_line(a||' Andrew Reid, International DBA');
  8  a := a + 1;
  9  end loop;
10  end;
11  /
1 Andrew Reid, International DBA
2 Andrew Reid, International DBA
3 Andrew Reid, International DBA
4 Andrew Reid, International DBA
5 Andrew Reid, International DBA
6 Andrew Reid, International DBA
7 Andrew Reid, International DBA
8 Andrew Reid, International DBA
9 Andrew Reid, International DBA
10 Andrew Reid, International DBA
11 Andrew Reid, International DBA
12 Andrew Reid, International DBA
13 Andrew Reid, International DBA
14 Andrew Reid, International DBA
15 Andrew Reid, International DBA
16 Andrew Reid, International DBA
17 Andrew Reid, International DBA
18 Andrew Reid, International DBA
19 Andrew Reid, International DBA
20 Andrew Reid, International DBA
21 Andrew Reid, International DBA
22 Andrew Reid, International DBA
23 Andrew Reid, International DBA
24 Andrew Reid, International DBA
25 Andrew Reid, International DBA
26 Andrew Reid, International DBA
27 Andrew Reid, International DBA
28 Andrew Reid, International DBA
29 Andrew Reid, International DBA
30 Andrew Reid, International DBA
31 Andrew Reid, International DBA
32 Andrew Reid, International DBA
33 Andrew Reid, International DBA
34 Andrew Reid, International DBA
35 Andrew Reid, International DBA
36 Andrew Reid, International DBA
37 Andrew Reid, International DBA
38 Andrew Reid, International DBA
39 Andrew Reid, International DBA
40 Andrew Reid, International DBA
41 Andrew Reid, International DBA
42 Andrew Reid, International DBA
43 Andrew Reid, International DBA
44 Andrew Reid, International DBA
45 Andrew Reid, International DBA
46 Andrew Reid, International DBA
47 Andrew Reid, International DBA
48 Andrew Reid, International DBA
49 Andrew Reid, International DBA
50 Andrew Reid, International DBA
51 Andrew Reid, International DBA
52 Andrew Reid, International DBA
53 Andrew Reid, International DBA
54 Andrew Reid, International DBA
declare
*
ERROR at line 1:
ORA-20000: ORU-10027: buffer overflow, limit of 2000
bytes
ORA-06512: at "SYS.DBMS_OUTPUT", line 35
ORA-06512: at "SYS.DBMS_OUTPUT", line 198
ORA-06512: at "SYS.DBMS_OUTPUT", line 139
ORA-06512: at line 7
 
SQL>