I was asked to write a simple procedure, that inserts a new row into a table under certaing condition. The database: table Users - user_id .... table Tracks - track_id .... table Liked - user_id, track_id
The procedure must ensure that a user is in users, the track is in tracks, but both don't appear in the same record in the table Liked.
Have no idea where the errors are from, please help!
List of errors: Error(72,1): PLS-00103: Encountered the symbol "DECLARE" Error(77,4): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
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;