gpt4 book ai didi

plsql - PL/SQL错误无法识别错误,PSL-00103

转载 作者:行者123 更新时间:2023-12-03 08:22:39 27 4
gpt4 key购买 nike

我被要求编写一个简单的过程,在一定条件下将新行插入到表中。
数据库:
表Users-user_id ....
表轨道-track_id ....
喜欢的表-user_id,track_id

该过程必须确保用户在用户中,轨道在轨道中,但是两者都不会出现在“喜欢”表的同一记录中。

不知道错误来自哪里,请帮忙!

错误 list :
错误(72,1):PLS-00103:遇到符号“DECLARE”
错误(77,4):PLS-00103:在预期以下情况之一时遇到符号“文件结束” :(如果使用循环<<继续关闭当前删除获取锁定插入打开回滚保存点设置sql执行提交forall合并管道清除

create or replace Procedure proc_insert_liked 
(p_userID IN liked.user_id%TYPE , p_trackID IN liked.track_id%TYPE)
AS
test1 liked.user_id%TYPE;
test2 liked.track_id%TYPE;
test3 tracks.track_id%Type;
test4 users.user_id%Type;

cursor c1 is
Select user_id
from liked
where user_id = p_userID;

cursor c2 is
Select track_id
from liked
where track_id = p_trackID;

cursor c3 is
Select track_id
from tracks
where track_id = p_trackID;

cursor c4 is
Select user_id
from users
where user_id = p_userID;

Begin

--opening cursors
open c1;
open c2;
open c3;
open c4;

fetch c1 into test1;
fetch c2 into test2;
fetch c3 into test3;
fetch c4 into test4;
-- if user and track in liked
if (c1%found) AND (c2%found) then
dbms_output.put_line('User already liked that song');
else
-- if track in tracks
if c3%found then
-- if user in users
if c4%found then
Insert into Liked (user_id, track_id)
Values (p_userId, p_trackID);
else
dbms_output.put_line('no such a user');
end if;
else
dbms_output.put_line('no such a track');
end if;
end if;


-- closing cursors
close c1;
close c2;
close c3;
close c4;

EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;


Declare
prompt_user_id liked.user_id%Type :=4795;
prompt_track_id liked.track_id%Type :=199589;
Begin
proc_insert_liked(prompt_user_id, prompt_track_id);
end;

最佳答案

如果将所有这些作为一个脚本运行,则需要在过程定义的/和匿名块的end;之间添加一个declare。您也应该在第二个end;之后添加一个以终止并提交匿名块,但是您的客户端似乎认为那里应该有一个。 (我猜这是SQL Developer吗?)

...
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;
/

Declare
prompt_user_id liked.user_id%Type :=4795;
...
end;
/

一旦启动PL/SQL块,分号仅是PL/SQL语句分隔符。即使在最后一个 end;之后,它们也不充当整个块的终端。

您可以使用 read more in the SQL*Plus reference,它在很大程度上也适用于SQL Developer。

顺便说一句,即使您通过自己的 when others报告实际错误,使用 raise捕获异常通常也不是一个好主意。您正在丢失将为您提供更多上下文并帮助您准确确定遇到错误的位置的堆栈跟踪。

关于plsql - PL/SQL错误无法识别错误,PSL-00103,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048415/

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