sql - PLS-00382 : expression is of wrong type in Oracle cursor

标签 sql oracle plsql cursor dynamicquery

我在oracle 11g中写了一个游标,如下:

DECLARE CURSOR QnA_cursor IS 
        SELECT activity_id, question, answer 
        FROM TABLE1
        WHERE question != 'surveyText'
        ORDER BY activity_id, question;

        cur_count       INT := 1;
        que             NVARCHAR2(10);
        ans             NVARCHAR2(10);
        sqlCommand      NVARCHAR2(500);
        RowCountVar     INT;
    BEGIN
        FOR QueAns
        IN QnA_cursor
        LOOP
            IF cur_count = 4 THEN cur_count := 1; END IF;   /* We have only 3 questions for each activity_id */
            que := 'question' || cur_count;     /* question1, question2, question3 */
            ans := 'answer' || cur_count;       /* answer1, answer2, answer3 */

            sqlCommand := 'UPDATE TABLE2 SET '||que||' = :1, '||ans||' = :2 WHERE activity_id = :3';
            EXECUTE IMMEDIATE sqlCommand USING QueAns.question, QueAns.answer, QueAns.activity_id;

            cur_count := cur_count + 1;
        END LOOP;
    END;

这是 TABLE1 和 TABLE2 的架构:

Create table TABLE2(
ACTIVITY_ID             NUMERIC(19,0),
QUESTION1               NVARCHAR2(2000),
ANSWER1                 NVARCHAR2(2000),
QUESTION2               NVARCHAR2(2000),
ANSWER2                 NVARCHAR2(2000),
QUESTION3               NVARCHAR2(2000),
ANSWER3                 NVARCHAR2(2000)
)

Create table TABLE1(
ACTIVITY_ID             NUMERIC(19,0),
QUESTION                NVARCHAR2(2000),
ANSWER                  NVARCHAR2(2000)
)

当我尝试在 sqldeveloper 中编译它时,在执行动态查询“EXECUTE IMMMEDIATE”的行处收到以下错误:

PLS-00382: expression is of wrong type

我已经尝试了很多次,但无法找出原因。两个表中相应列的数据类型相同。您能告诉我可能出现什么问题吗?

最佳答案

将 sqlCommand 的数据类型从 NVARCHAR2 更改为 varchar2。立即执行需要 varchar、varchar2 或 char。下面给出的代码应该可以工作

DECLARE
  CURSOR qna_cursor IS
    SELECT activity_id,
           question,
           answer
      FROM table1
     WHERE question != 'surveyText'
     ORDER BY activity_id,
              question;
  cur_count   INT := 1;
  que         VARCHAR2(10);
  ans         VARCHAR2(10);
  sqlcommand  VARCHAR2(500); --> Should be varchar, varchar2 or char
  rowcountvar INT;
BEGIN
  FOR queans IN qna_cursor LOOP
    IF cur_count = 4 THEN
      cur_count := 1;
    END IF; /* We have only 3 questions for each activity_id */
    que := 'question' || cur_count; /* question1, question2, question3 */
    ans := 'answer' || cur_count; /* answer1, answer2, answer3 */
    sqlcommand := 'UPDATE TABLE2 SET ' || que || ' = :1, ' || ans || ' = :2 WHERE activity_id = :3';
    EXECUTE IMMEDIATE sqlcommand USING queans.question, queans.answer, queans.activity_id;
    cur_count := cur_count + 1;
  END LOOP;
END;
/

关于sql - PLS-00382 : expression is of wrong type in Oracle cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998372/

相关文章:

Oracle SQL : select regexp_substr, "into clause is expected"

sql - Oracle SQLDeveloper : How to search which tables have a column matching given query?

mysql - 如何在MySQL中选择特定范围内具有特定条件的行?

oracle - 在 SQLAlchemy 中使用 Oracle 服务名称

oracle - ORA-04091 : table xx_xx is mutating, 触发器/函数可能看不到它

mysql - 将表连接到依赖于表的 View 时,这是 Oracle 中的错误吗

sql - TransactionScope IsolationLevel 未应用于 Azure

php - 1 个文件中的多个 SELECT 语句和 INSERTS

sql - 在 select 语句中使用一个单元格的值(使用 sqlcmd 运行)

python - Oracle SQL 计算列 - 如何在同一查询的另一个计算中引用计算列?