[英]Oracle/PL-SQl:Pass on query result to another update
Hi I am trying to update using PL/SQL based on one query result store the result and send this result to Update query but getting嗨,我正在尝试根据一个查询结果使用 PL/SQL 进行更新,存储结果并将此结果发送到更新查询,但得到
ORA-06550: line 2, column 24: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: := ( ; not null range default character
Table:桌子:
create table person(id number(10),stat char(1));
insert into person(id,stat)values(123,'Y');
insert into person(id,stat)values(123,'Y');
insert into person(id,stat)values(345,'Y');
commit;
Query:询问:
DECLARE
deptid person.id%TYPE;
BEGIN
SELECT id INTO deptid
FROM person WHERE id = 345;
IF SQL%FOUND THEN
Update person set stat='N' where id=deptid;
commit;
DBMS_OUTPUT.PUT_LINE('Dept Id: ' || deptid ||);
END IF;
END;
/
What am i doing wrong here?我在这里做错了什么?
Use a RETURNING
clause in the UPDATE
:在
UPDATE
使用RETURNING
子句:
DECLARE
deptid person.id%TYPE;
BEGIN
UPDATE person
SET stat = 'N'
WHERE id = 123
RETURNING MAX(id) INTO deptid;
DBMS_OUTPUT.PUT_LINE('Dept Id: ' || deptid);
END;
/
Which, for your test data:其中,对于您的测试数据:
create table person(id,stat) AS
SELECT 123, 'Y' FROM DUAL UNION ALL
SELECT 123, 'Y' FROM DUAL UNION ALL
SELECT 345, 'Y' FROM DUAL;
Then the PL/SQL block outputs:然后 PL/SQL 块输出:
Dept Id: 123
And the table contains the values:该表包含以下值:
\n ID |身份证 | STAT
统计\n--: |
--: | :---
:---\n123 |
123 | N
N \n123 |
123 | N
N \n345 |
第345话Y
是 \n
If you really want to use a SELECT
statement then you need to make sure it only returns a single row:如果你真的想使用
SELECT
语句,那么你需要确保它只返回一行:
DECLARE
deptid person.id%TYPE;
BEGIN
SELECT id
INTO deptid
FROM person
WHERE id = 123
AND ROWNUM = 1; -- FETCH FIRST ROW ONLY;
UPDATE person
SET stat = 'N'
WHERE id = deptid;
DBMS_OUTPUT.PUT_LINE('Dept Id: ' || deptid);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE( 'No Dept Id!' );
END;
/
Query that you posted as example has an error which cause code to fail.您作为示例发布的查询存在导致代码失败的错误。 Please remove extra pipes after deptid and before ')' in dbms_output command.
请在 dbms_output 命令中删除 deptid 之后和 ')' 之前的额外管道。
DBMS_OUTPUT.PUT_LINE('Dept Id: ' || deptid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.