[20180111]Oracle Bulk Collect and Limit2

lfree發表於2018-01-11

[20180111]Oracle's Bulk Collect and Limit2.txt

--//昨天在測試時遇到一個的奇怪問題,連結http://blog.itpub.net/267265/viewspace-2149969/,執行
--//@ limit 9 時,重複輸出了2次。主要是我的glogin配置檔案裡面包含如下:
set termout off
call dbms_output.enable(1e6);
set termout on

--//itpub論壇的提問:http://www.itpub.net/thread-2096243-1-1.html
--//預設設定 set serverout off,而且dbms_output設定為disable,而我在配置檔案中開啟了即使是set serverout off的情況下。
--//透過一個例子說明問題:

1.環境:
SCOTT@book> @ &r/ver1

PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> show serveroutput
serveroutput OFF

2.測試:

SCOTT@book> call dbms_output.enable(1e6);
Call completed.

SCOTT@book> exec DBMS_OUTPUT.put_line ('this is a first test!');
PL/SQL procedure successfully completed.

--//現在不顯示是正常的。

SCOTT@book> set serverout on
SCOTT@book> exec DBMS_OUTPUT.put_line ('this is a second test!');
this is a first test!
this is a second test!
PL/SQL procedure successfully completed.

--//OK,現在把在快取的第一行一起顯示出來。出現了2行。再次執行就不會了。

SCOTT@book> exec DBMS_OUTPUT.put_line ('this is a second test!');
this is a second test!
PL/SQL procedure successfully completed.


3.回到我當時的出現的問題就很好理解了。
我現在set serverout off情況下執行
set serverout off
@limit 9
set serverout on
@limit 9

--//這樣就出現2次顯示,感覺重複顯示了。主要問題我在set serverout off的情況下開啟了dbms_output.enable(1e6)。
--//而這樣設定源於去年的這個帖子:http://blog.itpub.net/267265/viewspace-2148591/=>[20171211]dbms_output無serveroutput on
--//這樣看來沒有想作者想像的這麼複雜,只要隨手呼叫1次exec DBMS_OUTPUT.put_line就ok了。

4.測試:
--//退出在登入:
SCOTT@book> exec dbms_output.enable(1e6);
PL/SQL procedure successfully completed.

SCOTT@book> exec dbms_output.put_line('first');
PL/SQL procedure successfully completed.

SCOTT@book> set serverout on
SCOTT@book> exec dbms_output.put_line(' ');
first

PL/SQL procedure successfully completed.

--//退出在登入:
SCOTT@book> exec dbms_output.enable(1e6);

PL/SQL procedure successfully completed.

SCOTT@book> @ limit 9
old  13:       FETCH get_emp_data BULK COLLECT INTO emp_c LIMIT &1;
new  13:       FETCH get_emp_data BULK COLLECT INTO emp_c LIMIT 9;
PL/SQL procedure successfully completed.

SCOTT@book> @ &r/output_print.sql
PL/SQL procedure successfully completed.
OUTPUT
---------------
7369SMITH800
7499ALLEN1600
7521WARD1250
7566JONES2975
7654MARTIN1250
7698BLAKE2850
7782CLARK2450
7788SCOTT3000
7839KING5000


10 rows selected.

--//這樣的好處不用設定set serverout on就能顯示。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2150028/,如需轉載,請註明出處,否則將追究法律責任。

相關文章