0

我有以下运行代码:

DECLARE
  rec_present NUMBER (10) := 0;
BEGIN
  SELECT COUNT (*)
  INTO rec_present
  FROM ref_cross_exchange_rate
  WHERE local_ccy_pk =
    &localCcy
  AND base_ccy_pk =
    &baseccy
  AND base_date =
    &BaseDate;
  IF rec_present = 0 THEN
    INSERT
    INTO ref_cross_exchange_rate
      (
        cross_exchange_rate_pk,
        base_date ,
        local_ccy_pk ,
        base_ccy_pk 
      )
      VALUES
      (
        (SELECT NVL (MAX (cross_exchange_rate_pk), 0) + 1
          FROM ref_cross_exchange_rate
        )
        ,
        &BaseDate,
        &localCcy,
        &baseccy
      );
    DBMS_OUTPUT.put_line ('1 RECORD INSERTED');
  ELSE
    DBMS_OUTPUT.put_line ('RECORD ALREADY EXISTS');
  END IF;
END;
/
COMMIT;

但问题是我必须多次输入相同的值,所以我做了一些修改如下:

DECLARE
  rec_present number (10) := 0;
  bdate Date          := &basedate;
  local_ccy     NUMBER(10,0)  := &localccy;
  base_ccy      NUMBER(10,0)  := &baseccy;
  exchange_rate NUMBER(20,10) := &ExchangeRate;
BEGIN
  SELECT COUNT (*)
  INTO rec_present
  from ref_cross_exchange_rate
  where local_ccy_pk = local_ccy
  and base_ccy_pk    = base_ccy
  AND base_date      =TO_DATE (bdate) ;
  IF rec_present     = 0 THEN
    INSERT
    INTO ref_cross_exchange_rate
      (
        cross_exchange_rate_pk,
        base_date ,
        local_ccy_pk ,
        base_ccy_pk 
      )
      VALUES
      (
        (SELECT NVL (MAX (cross_exchange_rate_pk), 0) + 1
          FROM ref_cross_exchange_rate
        )
        ,
        TO_DATE (bdate),
        local_ccy,
        base_ccy
      );
    DBMS_OUTPUT.put_line ('1 RECORD INSERTED');
  ELSE
    DBMS_OUTPUT.put_line ('RECORD ALREADY EXISTS');
  END IF;
END;
/
COMMIT;

但不幸的是它不起作用。我尝试了这个问题的建议,但它不起作用。

错误:

错误报告:

ORA-06550: line 3, column 26:
PLS-00382: expression is of wrong type
ORA-06550: line 3, column 9:
PL/SQL: Item ignored
ORA-06550: line 13, column 24:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 13, column 24:
PL/SQL: ORA-00904: "BDATE": invalid identifier
ORA-06550: line 8, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 40, column 9:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 40, column 9:
PL/SQL: ORA-00904: "BDATE": invalid identifier
ORA-06550: line 15, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
commited.
4

1 回答 1

0

在声明部分添加引号以正确地将值分配给变量。

这应该工作,

DECLARE

  rec_present number (10) := 0;
  bdate Date          := '&basedate';
  local_ccy     NUMBER(10,0)  := '&localccy';
  base_ccy      NUMBER(10,0)  := '&baseccy';
  exchange_rate NUMBER(20,10) := '&ExchangeRate';

BEGIN
  SELECT COUNT (*)
  INTO rec_present
  from ref_cross_exchange_rate
  where local_ccy_pk = local_ccy
  and base_ccy_pk    = base_ccy
  AND base_date      =TO_DATE (bdate) ;
  IF rec_present     = 0 THEN
    INSERT
    INTO ref_cross_exchange_rate
      (
        cross_exchange_rate_pk,
        base_date ,
        local_ccy_pk ,
        base_ccy_pk 
      )
      VALUES
      (
        (SELECT NVL (MAX (cross_exchange_rate_pk), 0) + 1
          FROM ref_cross_exchange_rate
        )
        ,
        TO_DATE (bdate),
        local_ccy,
        base_ccy
      );
    DBMS_OUTPUT.put_line ('1 RECORD INSERTED');
  ELSE
    DBMS_OUTPUT.put_line ('RECORD ALREADY EXISTS');
  END IF;
END;
/
COMMIT;
于 2013-02-06T05:27:46.940 回答