Oracle将自定义表类型传递给存储过程

时间:2016-03-18 17:25:46

标签: oracle stored-procedures custom-type

仅供参考: Oracle 12c

我创建了一个名为Payeezy_Error的自定义类型:

create or replace TYPE PAYEEZY_ERROR
AS

  OBJECT (
    CODE VARCHAR(30),
    DESCRIPTION VARCHAR(200)
  );

然后依次创建表格类型Payeezy_Errors

create or replace TYPE PAYEEZY_ERRORS AS TABLE OF PAYEEZY_ERROR;

然后我有一个将Payeezy_Errors作为IN参数的过程:

create or replace PROCEDURE SAVE_USER_PAYMENT_TRANSACTION
(
  in_AccountID         IN VARCHAR2,
  in_SequenceID        IN VARCHAR2,
  in_CorrelationID     IN VARCHAR2,
  in_TransactionID     IN VARCHAR2,
  in_TransactionTag    IN VARCHAR2,
  in_Currency          IN VARCHAR2,
  in_TransactionType   IN VARCHAR2,
  in_BankResponse      IN VARCHAR2,
  in_GatewayResponse   IN VARCHAR2,
  in_ValidationStatus  IN VARCHAR2,
  in_TransactionStatus IN VARCHAR2,
  in_Errors            IN PAYEEZY_ERRORS
)
AS

  var_uptID NUMBER;
  var_ErrorCount NUMBER := 0;

  EX_AUTHENTICATION EXCEPTION;

BEGIN

  -- Insert the Payeezy Response values tied to the user
  INSERT INTO
    USER_PAYMENT_TRANSACTION (
      ACCOUNT_ID,         UP_PAYMENT_SEQ_ID,   CORRELATION_ID,       TRANSACTION_ID,
      TRANSACTION_TAG,    CURRENCY,            TRANSACTION_TYPE,     BANK_RESPONSE,
      GATEWAY_RESPONSE,   VALIDATION_STATUS,   TRANSACTION_STATUS
    ) VALUES (
      in_AccountID,       in_SequenceID,       in_CorrelationID,     in_TransactionID,
      in_TransactionTag,  in_Currency,         in_TransactionType,   in_BankResponse,
      in_GatewayResponse, in_ValidationStatus, in_TransactionStatus
    )
  RETURNING
    ID
  INTO
    var_uptID;

  -- Insert any errors that may be associated with a failure/unsuccessful transaction
  SELECT
    COUNT(*)
  INTO
    var_ErrorCount
  FROM
    in_Errors;

  IF (var_ErrorCount > 0) THEN
    INSERT INTO
      USER_PAYMENT_TRANSACTION_ERROR (
        UPT_ID, CODE, DESCRIPTION
      )
    SELECT
      var_uptID, e.CODE, e.DESCRIPTION
    FROM
      in_Errors;
  END IF;


  -- Exception Handling
  EXCEPTION
    WHEN EX_AUTHENTICATION THEN
      raise_application_error(-20001, 'Authentication Failed.');

END SAVE_USER_PAYMENT_TRANSACTION;

当我编译程序时,它在SELECT COUNT(*)声明中对我大喊:

ORA-00942: table or view does not exist.

红色短划线位于SELECTin_Errors下。

在此过程中,我得到相同的错误,第二行INSERT INTOin_Errors行也是红色破折号。

我退出并重新加载Oracle SQL Developer只是为了看它是否是一个缓存的东西。我在网上搜索过但没有找到我的具体案例。

1 个答案:

答案 0 :(得分:1)

如果您想在查询中使用该表格,则需要使用table运算符

SELECT
    COUNT(*)
  INTO
    var_ErrorCount
  FROM
    table( in_Errors );

有效。但这意味着您将获取PL / SQL集合中的所有数据,将其移至SQL VM,执行聚合,然后将结果返回到PL / SQL。在这种情况下,只做

可能会更有效(而且代码更少)
var_ErrorCount := in_Errors.count;