PL / SQL编译错误 - PLS-00382:表达式类型错误

时间:2016-09-16 13:52:37

标签: oracle types plsql varchar varchar2

我正在运行以下PL / SQL ...

DECLARE
BEGIN 
FOR i IN (select VALUE from REWARDS)
LOOP 
insert into BT_CMS.T_REWARDS_TYPES 
(ID, REWARD_LABEL, REWARD_VALUE, REWARD_METHOD, UPDATE_USER, UPDATE_DATE, PAYMENT_PROVIDER_ID, CREATE_DATE, COUNTRY_CODE_ID) 
values 
(BT_CMS.SEQ_REWARD_TYPE_ID.nextval, 'R' || i || ' Real Time', i, 'Airtime', 'DEVOPS-826', sysdate, 120, sysdate, 206); 
END LOOP; 
END;

...并收到以下错误......

ORA-06550: line 8, column 72:
PLS-00382: expression is of wrong type
ORA-06550: line 8, column 52:
PLS-00382: expression is of wrong type
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.

我很确定问题在于我被替换为值,但我不知道究竟是什么问题。正在选择i的REWARDS表中的VALUE列具有data_type = VARCHAR2(20 BYTE)。我尝试将其插入的REWARD_LABEL列具有data_type = VARCHAR2(50 CHAR)。

1 个答案:

答案 0 :(得分:6)

在循环内部,i指的是整个记录,而不是记录的(唯一)字段;您需要使用i.value代替i

DECLARE
BEGIN
    FOR i IN (SELECT VALUE FROM REWARDS)
    LOOP
        INSERT INTO BT_CMS.T_REWARDS_TYPES(
                                           ID,
                                           REWARD_LABEL,
                                           REWARD_VALUE,
                                           REWARD_METHOD,
                                           UPDATE_USER,
                                           UPDATE_DATE,
                                           PAYMENT_PROVIDER_ID,
                                           CREATE_DATE,
                                           COUNTRY_CODE_ID
                                          )
             VALUES (
                     BT_CMS.SEQ_REWARD_TYPE_ID.NEXTVAL,
                     'R' || i.VALUE || ' Real Time',
                     i.VALUE,
                     'Airtime',
                     'DEVOPS-826',
                     SYSDATE,
                     120,
                     SYSDATE,
                     206
                    );
    END LOOP;
END;

更好的方法可能是使用单个insert-select而不是循环遍历游标;例如:

INSERT INTO BT_CMS.T_REWARDS_TYPES(
                                   ID,
                                   REWARD_LABEL,
                                   REWARD_VALUE,
                                   REWARD_METHOD,
                                   UPDATE_USER,
                                   UPDATE_DATE,
                                   PAYMENT_PROVIDER_ID,
                                   CREATE_DATE,
                                   COUNTRY_CODE_ID
                                  )
    SELECT BT_CMS.SEQ_REWARD_TYPE_ID.NEXTVAL,
           'R' || r.VALUE || ' Real Time',
           r.VALUE,
           'Airtime',
           'DEVOPS-826',
           SYSDATE,
           120,
           SYSDATE,
           206
      FROM REWARDS r