oracle系統包—-dbms_output用法

Ruthless發表於2013-06-24

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

注意陣列使用的格式。

相關文章