Valid XHTML 1.0 TransitionalOracle PL/SQL Review (P3)

Page: [index.html] (v2017-07-07_16-00)

  1. PL/SQL 基本概念:……
  2. PL/SQL 编程:
    1. 基本赋值、输入、输出:……
    2. 关于“块”:
      ……

      分类:
      1. 匿名块:……
      2. 命名块:
        ……

        分类:
        1. 过程:……
        2. 函数:
          “命名块”的一种,侧重于取得特定操作的返回值;
          支持参数,且必须有返回值。

          参数:……

          PL/SQL 规范并不鼓励用参数的 out 模式覆盖“块”外变量的值;本组用例中的 out 模式参数仅用于验证其“在语法上可行”。

          样例:
          1. 打印并返回系统日期:
            • 函数:
              -- Function block.
              create or replace function tfwFuncGetSysdateText
              	-- Area for argument(s).
              return varchar2
              as -- Block local variable(s) declaring.
              	vch2DateText varchar2(32);
              begin -- Mandatory core of the block.
              	dbms_output.put_line('Function - Getting system date text:');
              	vch2DateText := to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss');
              	dbms_output.put_line(vch2DateText);
              	return vch2DateText;
              end;
              / -- Block executing command?
              
              说明:
              • tfwFuncGetSysdateText”:“函数”的名称。
              • as”:对于“函数”,此处用“as”或“is”皆可。
            • 创建:
              [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 function tfwFuncGetSysdateText
                2  return varchar2
                3  as
                4     vch2DateText varchar2(32);
                5  begin
                6     dbms_output.put_line('Function - Getting system date text:');
                7     vch2DateText := to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss');
                8     dbms_output.put_line(vch2DateText);
                9     return vch2DateText;
               10  end;
               11  /
              
              函数已创建。
              
              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> variable vch2SysdateText varchar2(32);
              SQL> begin :vch2SysdateText := tfwFuncGetSysdateText();end;
                2  /
              
              PL/SQL 过程已成功完成。
              
              SQL> print vch2SysdateText;
              
              VCH2SYSDATETEXT
              --------------------------------
              2017-06-01 11:00:00
              
              
              SQL> set serveroutput on;
              SQL> call tfwFuncGetSysdateText() into :vch2SysdateText;
              Function - Getting system date text:
              2017-06-01 11:00:40
              
              调用完成。
              
              SQL> print vch2SysdateText;
              
              VCH2SYSDATETEXT
              --------------------------------
              2017-06-01 11:00:40
              
              SQL> exit;
              从 Oracle Database 11g Release 11.2.0.1.0 - 64bit Production 断开
              [typhoon@TFW-CENT6-LT typhoon]$
          2. 接收参日期格式字符串,返回系统日期格式化后的字符串:
            • 函数:
              -- Function block.
              create or replace function tfwFuncFormatSysdateToText
              	-- Block local variable(s) that binded to argument(s).
              	(vch2Format in varchar2)
              return varchar2
              as -- Block local variable(s) that not binded.
              	vch2SysdateText varchar2(32);
              	vch2ErrMsg varchar2(128);
              begin -- Mandatory core of the block.
              	dbms_output.put_line('Function - Format system date to text:');
              	dbms_output.put_line('Incoming format : ['||vch2Format||']');
              
              	-- Formatting system date.
              	vch2SysdateText := (to_char(systimestamp, vch2Format));
              	dbms_output.put_line('System date text: ['||vch2SysdateText||']');
              	return vch2SysdateText;
              exception -- Exception handler.
              	when others then -- No matter what exception it is.
              		vch2ErrMsg := dbms_utility.format_error_backtrace||' '||SQLERRM;
              		dbms_output.put_line('Exception: '||vch2ErrMsg);
              		-- raise; -- Similar to "throw" in Java; commented, not to throw in this sample.
              		return vch2ErrMsg;
              end;
              / -- Block executing command?
            • 创建:
            • 调用:
              SQL> set serveroutput on;
              SQL> variable vch2OuterSysdateText varchar2(32);
              SQL> call tfwFuncFormatSysdateToText('yyyy-mm-dd hh12:mm:ss am') into :vch2OuterSysdateText;
              Function - Format system date to text:
              Incoming format : [yyyy-mm-dd hh12:mm:ss am]
              System date text: [2017-06-01 01:00:20 下午]
              
              调用完成。
              
              SQL> print vch2OuterSysdateText;
              
              VCH2OUTERSYSDATETEXT
              --------------------------------
              2017-06-01 01:00:20 下午
              
              
              SQL> accept bufFormat prompt 'Enter date format: ';
              Enter date format: yyyy-mm-dd hh24:mm:ss.ff
              SQL> call tfwFuncFormatSysdateToText('&bufFormat') into :vch2OuterSysdateText;
              原值    1: begin :vch2OuterSysdateText := tfwFuncFormatSysdateToText('&bufFormat');end;
              新值    1: begin :vch2OuterSysdateText := tfwFuncFormatSysdateToText('yyyy-mm-dd hh24:mm:ss.ff');end;
              Function - Format system date to text:
              Incoming format : [yyyy-mm-dd hh24:mm:ss.ff]
              System date text: [2017-06-01 13:00:50.234711000]
              
              调用完成。
              
              SQL> print vch2OuterSysdateText;
              
              VCH2OUTERSYSDATETEXT
              --------------------------------
              2017-06-01 13:00:50.234711000
              
              
              SQL> variable vch2OuterSysdateText varchar2(128);
              SQL> call tfwFuncFormatSysdateToText('yyyy-mm-dd hh24:mm:ss.sss') into :vch2OuterSysdateText;
              Function - Format system date to text:
              Incoming format : [yyyy-mm-dd hh24:mm:ss.sss]
              Exception: ORA-06512: 在 "SCOTT.TFWFUNCFORMATSYSDATETOTEXT", line 13
               ORA-01821:
              日期格式无法识别
              
              调用完成。
              
              SQL> print vch2OuterSysdateText;
              
              VCH2OUTERSYSDATETEXT
              --------------------------------------------------------------------------------
              ORA-06512: 在 "SCOTT.TFWFUNCFORMATSYSDATETOTEXT", line 13
               ORA-01821: 日期格式无法识别
              
              
              SQL>
          3. 根据传入的参数值的长度决定是否将参数值覆盖为“长度超标”,并返回长度或错误码:
            • 函数:
              -- Function block.
              create or replace function tfwFuncOverrideWhileOverBytes
              	-- Area for argument(s).
              	(numBytesLimit in number, vch2Text in out varchar2)
              return number
              as -- Block local variable(s) declaring.
              	numBytes number := 0;
              begin -- Mandatory core of the block.
              	dbms_output.put_line('Function - 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;
              	return numBytes;
              exception when others then -- No matter what exception it is.
              	dbms_output.put_line('Exception: '||dbms_utility.format_error_backtrace||' '||SQLERRM);
              	return SQLCODE;
              	-- 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 := '';end;
                2  /
              
              PL/SQL 过程已成功完成。
              
              SQL> print vch2OuterText;
              
              VCH2OUTERTEXT
              --------------------------------
              
              
              SQL> variable numByteLength number;
              SQL> call tfwFuncOverrideWhileOverBytes(16, :vch2OuterText) into :numByteLength;
              Function - Override the over-16-byte(s) text:
              Incoming text is null.
              
              调用完成。
              
              SQL> print numByteLength;
              
              NUMBYTELENGTH
              -------------
                          0
              
              SQL> print vch2OuterText;
              
              VCH2OUTERTEXT
              --------------------------------
              
              
              
              SQL> begin :vch2OuterText := '一二三四五';end;
                2  /
              
              PL/SQL 过程已成功完成。
              
              SQL> call tfwFuncOverrideWhileOverBytes(16, :vch2OuterText) into :numByteLength;
              Function - Override the over-16-byte(s) text:
              Incoming 15 bytes.
              
              调用完成。
              
              SQL> print numByteLength;
              
              NUMBYTELENGTH
              -------------
                         15
              
              SQL> print vch2OuterText;
              
              VCH2OUTERTEXT
              --------------------------------
              一二三四五
              
              
              SQL> call tfwFuncOverrideWhileOverBytes(12, :vch2OuterText) into :numByteLength;
              Function - Override the over-12-byte(s) text:
              Incoming 15 bytes.
              Over 12 bytes!
              
              调用完成。
              
              SQL> print numByteLength;
              
              NUMBYTELENGTH
              -------------
                         15
              
              SQL> print vch2OuterText;
              
              VCH2OUTERTEXT
              --------------------------------
              Over 12 bytes!
              
              SQL>
        3. ……