oracle系统包—-dbms_output用法

dbms_output包主要用于调试pl/sql程序,或者在sql*plus命令中显示信息(displaying message)和报表,譬如我们可以写一个简单的匿名pl/sql程序块,而该块出于某种目的使用dbms_output包来显示一些信息。

涉及到的知识点如下:
1、enable:在serveroutput on的情况下,用来使dbms_output生效(默认即打开)
2、disable:在serveroutput on的情况下,用来使dbms_output失效
3、put:将内容写到内存,等到put_line时一起输出
4、put_line:不用多说了,输出字符
5、new_line:作为一行的结束,可以理解为写入buffer时的换行符
6、get_line(value, index):获取缓冲区的单行信息
7、get_lines(array, index):以数组形式来获取缓冲区的多行信息

需要注意以下几点:
1、set serveroutput on:如果要在sqlplus中看到dbms_output的输出,则必须设置该参数值为on
2、每行能容纳的最大值是32767bytes
3、buffer的默认值是20000bytes,可设置的最小值为2000bytes,最大值为1000000bytes

例子一、put和new_line

1
2
3
4
5
6
7
8
9
set serveroutput on;
begin
   dbms_output.put('a'); --写入buffer但不输出
   dbms_output.put('b'); --写入buffer但不输出
   dbms_output.new_line; --回车(换行),输出                             
   dbms_output.put_line('hello world!'); --输出并换行
   dbms_output.put('d'); --写入buffer但不输出
end;                                                    

执行运行结果:

1
2
ab
hello world!

例子二、put_line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
set serveroutput off;
create table t(a int, b int, c int);
insert into t values(111111,222222,333333);
insert into t values(444444,555555,666666);
insert into t values(777777,888888,999999);
commit;
 
create table tt(a int,b varchar2(100));
 
declare
   msg varchar2(120);                              
   cursor t_cur is select * from t order by a;     
   v_line varchar2(100);                           
   v_status integer := 0;                          
begin                                           
   dbms_output.enable;                             
   for i in t_cur loop                             
       msg := i.a || ',' || i.b || ',' || i.c;         
       dbms_output.put_line(msg); --put                  
   end loop;                                      
                                                  
   dbms_output.get_line(v_line, v_status); --get         
   while v_status = 0 loop                        
       insert into tt values(v_status, v_line);       
       dbms_output.get_line(v_line, v_status);         
   end loop;                                      
end;                                           
/                                              
              
select * from tt;

执行结果如下:

1
2
3
4
5
a    b
--- -----------------------
0   111111,222222,333333
0   444444,555555,666666
0   777777,888888,999999

注:使用get_line时不能用put_line输出,因为put_line之后会将buffer清空。(当然在serveroutput off的情况下put_line是不影响buffer的)。

例子三:put_lines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
set serveroutput on;
declare
   v_data dbms_output.chararr;                         
   v_numlines number;                                  
begin                                               
   --enable the buffer first.                         
   dbms_output.enable(1000000);                        
                                                      
   dbms_output.put_line('line one');                   
   dbms_output.put_line('line two');                   
   dbms_output.put_line('line three');                
                                                      
   v_numlines := 3;                                   
   dbms_output.get_lines(v_data, v_numlines);  --array, index       
   for v_counter in 1..v_numlines loop                
       dbms_output.put_line(v_data(v_counter));           
   end loop;                                          
end;                                               
/

执行结果如下:

1
2
3
line one
line two
line three

注意数组使用的格式。

posted on 2013-06-24 16:01  Ruthless  阅读(36547)  评论(0编辑  收藏  举报