gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:16:28 28 4
gpt4 key购买 nike

我在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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com