begin -- Executive code, MANDATORY! end;
-- Procedure block. create or replace procedure tfwProcPrintSysdateText -- Area for argument(s). as -- Block local variable(s) declaring. begin -- Mandatory core of the block. dbms_output.put_line('Procedure - System date printing:'); dbms_output.put_line(to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss')); end; / -- Block executing command?说明:
[typhoon@TFW-CENT6-LT ~]$ NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8" ORACLE_SID=███ sqlplus ███/███; SQL*Plus: Release 11.2.0.1.0 Production on █████ █ ████ ██ Copyright (c) 1982, 2009, Oracle. All rights reserved. 连接到: Oracle Database 11g Release 11.2.0.1.0 - 64bit Production SQL> create or replace procedure tfwProcPrintSysdateText 2 as 3 begin 4 dbms_output.put_line('Procedure - System date printing:'); 5 dbms_output.put_line(to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss')); 6 end; 7 / 过程已创建。 SQL> desc tfwProcPrintSysdateText; PROCEDURE tfwProcPrintSysdateText SQL> show errors; 没有错误。 SQL> exit; 从 Oracle Database 11g Release 11.2.0.1.0 - 64bit Production 断开 [typhoon@TFW-CENT6-LT ~]$
[typhoon@TFW-CENT6-LT ~]$ NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8" ORACLE_SID=███ sqlplus ███/███; SQL*Plus: Release 11.2.0.1.0 Production on █████ █ ████ ██ Copyright (c) 1982, 2009, Oracle. All rights reserved. 连接到: Oracle Database 11g Release 11.2.0.1.0 - 64bit Production SQL> set serveroutput on; SQL> exec tfwProcPrintSysdateText; Procedure - System date printing: 2017-05-30 20:30:10 PL/SQL 过程已成功完成。 SQL>说明:
SQL> call tfwProcPrintSysdateText();
Procedure - System date printing:
2017-05-30 20:30:50
PL/SQL 过程已成功完成。
SQL> exit;
从 Oracle Database 11g Release 11.2.0.1.0 - 64bit Production 断开
[typhoon@TFW-CENT6-LT typhoon]$
说明:-- Procedure block. create or replace procedure tfwProcFmtAndAssignSysdateText -- Block local variable(s) that binded to argument(s). (vch2Format in varchar2, vch2SysdateTextCopy out varchar2) as -- Block local variable(s) that not binded. vch2FormatCopy varchar2(32); vch2SysdateText varchar2(32); begin -- Mandatory core of the block. dbms_output.put_line('Procedure - Format and assign system date as text:'); dbms_output.put_line('Incoming format : ['||vch2Format||']'); -- Assigning into INNER-BLOCK VARIAVLE. vch2FormatCopy := vch2Format; dbms_output.put_line('Copied format : ['||vch2FormatCopy||']'); -- Getting, formatting system date into an INNER-BLOCK VARIAVLE. vch2SysdateText := (to_char(systimestamp, vch2Format)); dbms_output.put_line('System date text: ['||vch2SysdateText||']'); -- Assigning into INNER-BLOCK VARIAVLE to send out. vch2SysdateTextCopy := vch2SysdateText; dbms_output.put_line('Text to send out: ['||vch2SysdateTextCopy||']'); exception -- Exception handler. when others then -- No matter what exception it is. vch2SysdateTextCopy := dbms_utility.format_error_backtrace||' '||SQLERRM; dbms_output.put_line('Exception: '||vch2SysdateTextCopy); -- raise; -- Similar to "throw" in Java; commented, not to throw in this sample. end; / -- Block executing command?
SQL> set serveroutput on;
SQL> variable vch2OuterSysdateText varchar2(32);
SQL> call tfwProcFmtAndAssignSysdateText('yyyy-mm-dd hh12:mm:ss am', :vch2OuterSysdateText);
Procedure - Format and assign system date as text:
Incoming format : [yyyy-mm-dd hh12:mm:ss am]
Copied format : [yyyy-mm-dd hh12:mm:ss am]
System date text: [2017-05-31 06:10:20 下午]
Text to send out: [2017-05-31 06:10:20 下午]
PL/SQL 过程已成功完成。
SQL> print vch2OuterSysdateText;
VCH2OUTERSYSDATETEXT
--------------------------------
2017-05-31 06:10:20 下午
SQL>
SQL> set serveroutput on;
SQL> variable vch2OuterSysdateText varchar2(32);
SQL> accept bufFormat prompt 'Enter date format: ';
Enter date format: yyyy-mm-dd hh24:mm:ss.ff
SQL> call tfwProcFmtAndAssignSysdateText('&bufFormat', :vch2OuterSysdateText);
Procedure - Format and assign system date as text:
Incoming format : [yyyy-mm-dd hh24:mm:ss.ff]
Copied format : [yyyy-mm-dd hh24:mm:ss.ff]
System date text: [2017-05-31 18:40:20.169049000]
Text to send out: [2017-05-31 18:40:20.169049000]
PL/SQL 过程已成功完成。
SQL> print vch2OuterSysdateText;
VCH2OUTERSYSDATETEXT
--------------------------------
2017-05-31 18:40:20.169049000
SQL>
SQL> set serveroutput on; variable vch2OuterSysdateText varchar2(128); call tfwProcFmtAndAssignSysdateText('yyyy-mm-dd hh24:mm:ss.sss', :vch2OuterSysdateText); print vch2OuterSysdateText; SQL> SQL> Procedure - Format and assign system date as text: Incoming format : [yyyy-mm-dd hh24:mm:ss.sss] Copied format : [yyyy-mm-dd hh24:mm:ss.sss] Exception: ORA-06512: 在 "SCOTT.TFWPROCFMTANDASSIGNSYSDATETEXT", line 16 ORA-01821: 日期格式无法识别 PL/SQL 过程已成功完成。 SQL> print vch2OuterSysdateText; VCH2OUTERSYSDATETEXT -------------------------------------------------------------------------------- ORA-06512: 在 "SCOTT.TFWPROCFMTANDASSIGNSYSDATETEXT", line 16 ORA-01821: 日期格式无法识别 SQL>说明:
create or replace procedure tfwProcAcceptAndAssign
(vch2Text out varchar2)
as
begin
dbms_output.put_line('Procedure - Accept value from prompt, then assign to OUTER-BLOCK VARIABLE, then print:');
vch2Text := '&bufOfText';
dbms_output.put_line('Value of [vch2Text] : ['||vch2Text||']');
exception when others then
dbms_output.put_line('Exception: '||dbms_utility.format_error_backtrace||' '||SQLERRM);
end;
/
说明:create or replace procedure tfwProcAcceptInBlock
(vch2Text out varchar2)
as
begin
dbms_output.put_line('Procedure - Accepting IN block:');
accept bufText_ prompt 'Enter text (length ≤ 16) here: ';
vch2Text := '&bufText_';
dbms_output.put_line('Value of [vch2Text] : ['||vch2Text||']');
exception when others then
dbms_output.put_line('Exception: '||dbms_utility.format_error_backtrace||' '||SQLERRM);
end;
/
说明:-- Procedure block. create or replace procedure tfwProcOverrideWhileOverBytes -- Block local variable(s) that binded to argument(s). (numBytesLimit in number, vch2Text in out varchar2) as -- Block local variable(s) that not binded. numBytes number; begin -- Mandatory core of the block. dbms_output.put_line('Procedure - Override the over-'||numBytesLimit||'-byte(s) text:'); if (vch2Text is null) then dbms_output.put_line('Incoming text is null.'); elsif (''=vch2Text) then dbms_output.put_line('Incoming text is empty.'); else numBytes := lengthb(vch2Text); dbms_output.put_line('Incoming '||numBytes||' bytes.'); if (numBytesLimit<numBytes) then vch2Text := 'Over '||numBytesLimit||' bytes!'; dbms_output.put_line(vch2Text); end if; end if; exception when others then -- No matter what exception it is. dbms_output.put_line('Exception: '||dbms_utility.format_error_backtrace||' '||SQLERRM); -- raise; -- Similar to "throw" in Java; commented, not to throw in this sample. end; / -- Block executing command?
SQL> set serveroutput on; SQL> variable vch2OuterText varchar2(16); SQL> begin :vch2OuterText := null;end; 2 / PL/SQL 过程已成功完成。 SQL> print vch2OuterText; VCH2OUTERTEXT -------------------------------- SQL> call tfwProcOverrideWhileOverBytes(16, :vch2OuterText); Procedure - Override the over-16-byte(s) text: Incoming text is null. 调用完成。 SQL> print vch2OuterText; VCH2OUTERTEXT --------------------------------
SQL> begin :vch2OuterText := '一二三四五';end; 2 / PL/SQL 过程已成功完成。 SQL> print vch2OuterText; VCH2OUTERTEXT -------------------------------- 一二三四五 SQL> call tfwProcOverrideWhileOverBytes(16, :vch2OuterText); Procedure - Override the over-16-byte(s) text: Incoming 15 bytes. 调用完成。 SQL> print vch2OuterText; VCH2OUTERTEXT -------------------------------- 一二三四五
SQL> call tfwProcOverrideWhileOverBytes(12, :vch2OuterText); Procedure - Override the over-12-byte(s) text: Incoming 15 bytes. Over 12 bytes! 调用完成。 SQL> print vch2OuterText; VCH2OUTERTEXT -------------------------------- Over 12 bytes! SQL>