[每日一题]  11gOCP 1z0-052 :2013-09-9  PL/SQL package body...........................................A46-LMLPHP


 正确答案:AD

          这张图描述了包规范和包主体。表明包规规范提当着包主体的接口。包规范和包主体中都可以声明变量丶类型和组件。在包规规范中声明的变量丶类型和组件会被发布,而仅在包主体内声明的变量丶类型和组件则是局部组件。在组件实现内定义的命名块是私有模块,或者是局部函数和过程的黑盒内的部分。

        当在包规范中声明了包函数和过程时,外部PL/SQL块可调用它们。仅在包体主体中声明的组件通过它们的包声明调用发布组件。

       当在包规范中定义用户自定义数据类型时,它们就对其它所有PL/SQL块公有可用。

       当在包体中定义用户自定义数据类型时,它们就对在包体中实现的PL/SQL块私有可用。

      无论使用的是包规范还是包主体,变量和类型的实现方法式都是相同的。

         [每日一题]  11gOCP 1z0-052 :2013-09-9  PL/SQL package body...........................................A46-LMLPHP  

          这张图是说明黑盒的重载的工作方。在第一个签名中,第二个参数是CLOB,第三个参数是DATE,而它们在第二个签名中的位置调了个儿。示例ADDING函数的图将VARCHAR2参数显示成两个圆漏斗,将NUMBER参数显示成两个方漏斗。

     [每日一题]  11gOCP 1z0-052 :2013-09-9  PL/SQL package body...........................................A46-LMLPHP

包主体:

      包主体包含在包规范中声明实现.一切都必须完全相符,包括形参的默认值。在Oracle9i之前可以在规范中声明形参,但是不完全强制在包主体中实现它们。所以从Oracle8i迁移到更新的版本之前需要检查一下(如果还没有检查的话)代码某些部分。

       包主体原型与包规范原型非常相似。包主体几乎可以声明包规范设置的一切,除了一件事外:不能在包主体内定义函数的PRAAGMA。任何这样的企图都会引发PLS-00708错误,该项错误表明必须把它们放在包规范中。

       只要不同于包规范中所声明的内空,就可以使用包级异常的EXCEPTION_INIT PRAGMA指令。也可以重写在包规范中声明的一个变量。可以通过在包主体中再次声明这个变量来重写。这样,就使包主体不能访问变量的这两个副本。当试图编制译包主体时,在包主体内对声明了两次变量的任何引用都会引发PLS-00371异常。该异常表明,至多只允许声明变量一次。其意图似乎是表明不应当利用这个行为,而包主体的成功编译很可能是一个潜在的BUG。

       在包主体中声明的包级变量不同于在包规中声明的包级变量。不能在包之外访问包级变量。只有包规范发布的函数和过程可以访问包级变量。这使得这些变量非常像实例变量,因为它们在对包函数与过程的调用之间保持了状态。至少,从第一次调用直到会话结束或因为留时间过长被推出SGA,它们保持了它们的状态。

举个实际工作中的例子:

创建包规范:

 CREATE OR REPLACE PACKAGE EBS_TRADE IS

	-- ?ú???í?ó????
	PROCEDURE MakeErrorMessage(Head IN EBSHEADOBJECT, ErrorCode IN VARCHAR2,
		ResBuffer OUT VARCHAR2);
PROCEDURE MakeCancelMessage(Head IN EBSHEADOBJECT, ErrorCode IN VARCHAR2,
		ResBuffer IN VARCHAR2);

	PROCEDURE PrintLogInfo(Head IN EBSHEADOBJECT, Info IN VARCHAR2);

  -- ????????
	FUNCTION EncryptPassword(iOldPass IN VARCHAR2) RETURN VARCHAR2;
    FUNCTION EncryptPassword6(iOldPass IN VARCHAR2) RETURN VARCHAR2;

  	-- ???????¨??????
	FUNCTION EncryptAuth(iAuthCode IN VARCHAR2,
		iHdCode IN VARCHAR2) RETURN VARCHAR2;

  	-- ????6?????ú??
	FUNCTION GetRandNum RETURN NUMBER;

    PROCEDURE WriteElapsed(Head IN EBSHEADOBJECT);
END EBS_TRADE;

创建包主体

CREATE OR REPLACE PACKAGE BODY EBS_TRADE IS
	-- ???????????????í?ó????
	PROCEDURE MakeErrorMessage(Head IN EbsHeadObject, ErrorCode IN VARCHAR2,
		ResBuffer OUT VARCHAR2)
	AS
		PRAGMA AUTONOMOUS_TRANSACTION;
        time_end NUMBER;
	BEGIN
		time_end:=DBMS_UTILITY.GET_TIME;
        UPDATE BASE_STREAM_LOG SET STATE = 1,RETCODE=ErrorCode,ELAPSED=(time_end-Head.time_begin) WHERE STREAMID = Head.StreamId;
		-- ?????????????????????á??
		COMMIT;
		SELECT LPAD(Head.serno,13,\'0\') || RETCODE || RETMESSAGE INTO ResBuffer FROM BASE_RET_INFO
			WHERE RETCODE = ErrorCode;
	EXCEPTION
		WHEN NO_DATA_FOUND THEN
		ResBuffer := LPAD(Head.serno,13,\'0\') ||ErrorCode || \'?????í?ó?ú??\';
	END MakeErrorMessage;
	PROCEDURE MakeCancelMessage(Head IN EbsHeadObject, ErrorCode IN VARCHAR2,
		ResBuffer IN VARCHAR2)
	AS
		PRAGMA AUTONOMOUS_TRANSACTION;
        time_end NUMBER;
	BEGIN
		time_end:=DBMS_UTILITY.GET_TIME;
        UPDATE BASE_STREAM_LOG SET STATE = 1,RETCODE=ErrorCode,ELAPSED=(time_end-Head.time_begin) WHERE STREAMID = Head.StreamId;
		-- ?????????????????????á??
		COMMIT;
		--SELECT LPAD(Head.serno,13,\'0\') || RETCODE || RETMESSAGE INTO ResBuffer FROM BASE_RET_INFO
		--	WHERE RETCODE = ErrorCode;
	EXCEPTION
		WHEN NO_DATA_FOUND THEN
		--ResBuffer := LPAD(Head.serno,13,\'0\') ||ErrorCode || \'?????í?ó?ú??\';
        ROLLBACK;
        RETURN;
	END MakeCancelMessage;
PROCEDURE WriteElapsed(Head IN EbsHeadObject)
	AS
		PRAGMA AUTONOMOUS_TRANSACTION;
        time_end NUMBER;
	BEGIN
		time_end:=DBMS_UTILITY.GET_TIME;
        UPDATE BASE_STREAM_LOG SET ELAPSED=(time_end-Head.time_begin)||\'????\' WHERE STREAMID = Head.StreamId;
		-- ?????????????????????á??
		COMMIT;
	END WriteElapsed;

	PROCEDURE PrintLogInfo(Head IN EBSHEADOBJECT, Info IN VARCHAR2)
	AS
		FileHandle UTL_FILE.file_type;
		HeadInfo VARCHAR(2000);
	BEGIN
		FileHandle := UTL_FILE.fopen(\'FILESERVERDIR\',\'PrintLogInfo.dbg\', \'a\', 2000);
        HeadInfo := \'[???×\' || Head.TradeType || Head.TradeCode
				|| \'][?á?°\' || sys_context(\'USERENV\', \'SESSIONID\')
				|| \'][?±??\'|| to_char(SYSDATE, \'YYYY-MM-DD HH24:MI:SS\') || \']\';
        UTL_FILE.put_line(FileHandle,  HeadInfo);
        UTL_FILE.put_line(FileHandle, \'    \' || Info);
        UTL_FILE.fflush(FileHandle);
        UTL_FILE.fclose(FileHandle);
    END PrintLogInfo;

    FUNCTION EncryptPassword6(iOldPass IN VARCHAR2) RETURN VARCHAR2 IS
        Result VARCHAR2(6);
        Temp VARCHAR2(6);
        c CHAR(1);
        c1 CHAR(1);
        c2 CHAR(1);
        c3 CHAR(1);
        c4 CHAR(1);
        c5 CHAR(1);
        c6 CHAR(1);
    BEGIN
        IF LENGTHB(iOldPass) < 6 THEN
            Temp := LPAD(iOldPass, 6, \'0\');
        ELSIF LENGTHB(iOldPass) > 6 THEN
            Temp := SUBSTRB(iOldPass, 1, 6);
        ELSE
            Temp := iOldPass;
        END IF;

        c := SUBSTRB(Temp, 5, 1);
        IF c = \'0\' THEN
        c1 := \'0\';
        ELSE
        c1 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 4, 1);
        IF c = \'0\' THEN
        c2 := \'0\';
        ELSE
        c2 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 1, 1);
        IF c = \'0\' THEN
        c3 := \'0\';
        ELSE
        c3 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 6, 1);
        IF c = \'0\' THEN
        c4 := \'0\';
        ELSE
        c4 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 3, 1);
        IF c = \'0\' THEN
        c5 := \'0\';
        ELSE
        c5 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 2, 1);
        IF c = \'0\' THEN
        c6 := \'0\';
        ELSE
        c6 := CHR(106 - ASCII(c));
        END IF;

        Result := c1 || c2 || c3 || c4 || c5 || c6;

        RETURN Result;
    END EncryptPassword6;

    -- ????????
	FUNCTION EncryptPassword(iOldPass IN VARCHAR2) RETURN VARCHAR2 IS
		Result VARCHAR2(8);
        Temp VARCHAR2(8);
        c CHAR(1);
        c1 CHAR(1);
        c2 CHAR(1);
        c3 CHAR(1);
        c4 CHAR(1);
        c5 CHAR(1);
        c6 CHAR(1);
        C7 CHAR(1);
        C8 CHAR(1);
	BEGIN
        IF LENGTHB(iOldPass) < 8 THEN
    	    Temp := LPAD(iOldPass, 8, \'0\');
        ELSIF LENGTHB(iOldPass) > 8 THEN
        	Temp := SUBSTRB(iOldPass, 1, 8);
        ELSE
        	Temp := iOldPass;
        END IF;

        c := SUBSTRB(Temp, 5, 1);
        IF c = \'0\' THEN
        c1 := \'0\';
        ELSE
        c1 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 4, 1);
        IF c = \'0\' THEN
        c2 := \'0\';
        ELSE
        c2 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 1, 1);
        IF c = \'0\' THEN
        c3 := \'0\';
        ELSE
        c3 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 6, 1);
        IF c = \'0\' THEN
        c4 := \'0\';
        ELSE
        c4 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 3, 1);
        IF c = \'0\' THEN
        c5 := \'0\';
        ELSE
        c5 :=CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 2, 1);
        IF c = \'0\' THEN
        c6 := \'0\';
        ELSE
        c6 := CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 7, 1);
        IF c = \'0\' THEN
        c7 := \'0\';
        ELSE
        c7 := CHR(106 - ASCII(c));
        END IF;

        c := SUBSTRB(Temp, 8, 1);
        IF c = \'0\' THEN
        c8 := \'0\';
        ELSE
        c8 := CHR(106 - ASCII(c));
        END IF;
        Result := c1 || c2 || c3 || c4 || c5 || c6 || c7 || c8 ;

        RETURN Result;
	END EncryptPassword;
    /*?ú?????ú??*/
    FUNCTION GetRandNum RETURN NUMBER
		IS
		  	ResultNumber NUMBER(6);
		BEGIN
			ResultNumber := CEIL(DBMS_RANDOM.VALUE() * 1000000);
			IF ResultNumber < 100000 THEN
			   ResultNumber := ResultNumber + 100000;
			END IF;
			RETURN ResultNumber;
	END GetRandNum;


    	-- ???????¨??
 	FUNCTION EncryptAuth(
		iAuthCode IN VARCHAR2, iHdCode IN VARCHAR2) RETURN VARCHAR2
 	AS
         AuthCode2 VARCHAR2(6);
         SoundChar VARCHAR2(32);
         TransChar VARCHAR2(32);
 	BEGIN
		AuthCode2 := EBS_TRADE.EncryptPassword6(iAuthCode);
		SoundChar := SOUNDEX(iHdcode);
		TransChar := TRANSLATE(iHdcode, \'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\',
			\'0123456789\');
		RETURN SoundChar || substr(iHdCode, 1, 3) || AuthCode2|| TransChar;
 	END EncryptAuth;
END;




12-14 12:30