[英]Why am I getting this error: “ORA-00922: missing or invalid option” after running a PL/SQL procedure?
I created a table Patient with some attributes, like p_name, p_surname, p_number... I would like to create a procedure to transfer a patient from this table Patient to another table (Patient_backup), case his "p_number" attribute has received an input, deleting it from the first table and remaining only in the second.我创建了一个带有一些属性的表 Patient,例如 p_name、p_surname、p_number ... ,将其从第一个表中删除,仅保留在第二个表中。 The second table has the same structure of the first one.
第二个表与第一个表的结构相同。 I have coded the procedure like that.
我已经对这样的程序进行了编码。
CREATE TABLE patient (
p_number VARCHAR2(10) NOT NULL,
p_name VARCHAR2(15),
p_surname VARCHAR2(15),
p_street VARCHAR2(20),
p_city VARCHAR2(15)
);
CREATE TABLE patient_backup (
p_number VARCHAR2(10) NOT NULL,
p_name VARCHAR2(15),
p_surname VARCHAR2(15),
p_street VARCHAR2(20),
p_city VARCHAR2(15)
);
CREATE [OR REPLACE] PROCEDURE transfer (p_number VARCHAR2)
AS
CURSOR k1 IS SELECT p_number FROM patient;
BEGIN
OPEN k1;
LOOP
FETCH k1 INTO p_number;
IF p_number IS NULL THEN
dbms_output.put_line('empty');
ELSE
INSERT INTO patient_backup (SELECT * FROM patient);
Execute Immediate 'Drop Table patient;';
END IF;
END LOOP;
CLOSE k1;
END transfer;
But when I run it,I get the error "ORA-00922: missing or invalid option".但是当我运行它时,我收到错误“ORA-00922:缺少或无效选项”。 Could you help me with that?
你能帮我解决这个问题吗? I wonder if the code is correct.
我想知道代码是否正确。 I have read a material about PL/SQL, but the concepts were not connected to each other, so I just tried to gather everything together, and I hope it is correct.
我读过一篇关于 PL/SQL 的资料,但是这些概念之间并没有相互联系,所以我只是尝试将所有内容收集在一起,希望它是正确的。 Could you help me to correct this code and make it work?
你能帮我更正这段代码并让它工作吗?
It's hard to tell where exactly the error is, but my guess is: remove the ;
很难说出错误的确切位置,但我的猜测是:删除
;
from inside the string for execute immediate
.从字符串内部
execute immediate
。
But I think you want do not want to DROP
the table - that removes the table completely from the database including all rows and its definition.但我认为你不想
DROP
表 - 从数据库中完全删除表,包括所有行及其定义。 It won't be accessible after that.之后就无法访问了。
I think what you really want is to DELETE
a row from that table, not remove the table completely.我认为您真正想要的是从该表中
DELETE
一行,而不是完全删除该表。
Also: the whole loop is completely unnecessary (and inefficient).另外:整个循环是完全没有必要的(而且效率低下)。 You can do that with two simple SQL statements:
您可以使用两个简单的 SQL 语句来做到这一点:
insert into patient_backup
select *
from patient
where p_number = 42; --<< to pick one patient
delete from patient
where p_number = 42;
Putting that into a procedure:将其放入程序中:
CREATE PROCEDURE transfer (p_number_to_delete VARCHAR2)
AS
BEGIN
insert into patient_backup
select *
from patient
where p_number = p_number_to_delete;
delete from patient
where p_number = p_number_to_delete;
END transfer;
It's highly recommended to not use the name of a column as the name of a parameter.强烈建议不要使用列名作为参数名。 That's why I named the parameter
p_number_to_delete
(but p_number
is a bad name for a column that isn't a number to begin with - but that's a different discussion)这就是我将参数命名为
p_number_to_delete
的原因(但p_number
对于不是以数字开头的列来说是一个坏名称 - 但这是一个不同的讨论)
I think you need to DECLARE
your cursor before you define it.我认为您需要在定义
DECLARE
之前声明它。
In Your procedure code have some error在您的程序代码中有一些错误
1.P_NUMBER input parameter cannot be used into statment 2.don't use semicolon inside the EXECUTE IMMEDIATE string 3. in loop statement you should use exit otherwise it will run continuously 1.P_NUMBER 输入参数不能用于语句 2.不要在 EXECUTE IMMEDIATE 字符串中使用分号 3.在循环语句中你应该使用 exit 否则它将连续运行
Here the code这里的代码
CREATE OR REPLACE PROCEDURE TRANSFER (P_NUMBER IN VARCHAR2) AS
CURSOR K1 IS
SELECT P_NUMBER FROM PATIENT;
P_NUM PLS_INTEGER;
BEGIN
OPEN K1;
LOOP
FETCH K1 INTO P_NUM;
IF P_NUM IS NULL THEN
DBMS_OUTPUT.PUT_LINE('EMPTY');
ELSE
INSERT INTO PATIENT_BACKUP (SELECT * FROM PATIENT);
DELETE FROM PATIENT;
END IF;
EXIT WHEN P_NUM IS NULL;
END LOOP;
CLOSE K1;
END TRANSFER;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.