Using "BEGIN" keyword improperly - sql

I am getting an error in the code below. I have added an arror <------ where the problem is.
The error message says that a THEN is expected, but when I use THEN, then it says that a BEGIN is expected.
Error(27,6): PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: then and or The symbol "then" was substituted for "BEGIN" to continue.
Error(30,6): PLS-00103: Encountered the symbol "END" when expecting one of the following: , ; return returning
What am I doing wrong?
create or replace PROCEDURE "sp_updateUserPassword"(newUserPwd IN VARCHAR2, curIsoUserUID IN NUMBER)
IS
curUserID NUMBER;
userDateCreated DATE;
oldUserPwd VARCHAR2(255);
BEGIN
SELECT ISOUID INTO curUserID FROM ISOUSERS WHERE ISOUID=curIsoUserUID;
SELECT DATECREATE INTO userDateCreated FROM ISOUSERS WHERE ISOUID=curIsoUserUID;
SELECT PASSWORD INTO oldUserPwd FROM ISOUSERS WHERE ISOUID=curIsoUserUID;
IF(newUserPwd = oldUserPwd)
THEN
raise_application_error(-20000, 'The new password must be different from the previous password');
RETURN;
END IF;
IF NOT EXISTS
(
SELECT ISOUID
FROM OLDUSERPASSWORDS
WHERE ISOUID=curIsoUserUID
)
BEGIN <------------ Error is here
INSERT INTO OLDUSERPASSWORDS(ISOUID, DATECREATE, DATELASTCHANGE, CURRENTPASS, OLDPASS)
VALUES(curUserID, userDateCreated, SYSDATE, newUserPwd, oldUserPwd)
END;
/*raise_application_error(-20000, 'TEST');*/
END "sp_updateUserPassword";
Update
Corrected the code, so it now looks like this:
IF NOT EXISTS
(
SELECT ISOUID
FROM OLDUSERPASSWORDS
WHERE ISOUID=curIsoUserUID
)
THEN
BEGIN
INSERT INTO OLDUSERPASSWORDS(ISOUID, DATECREATE, DATELASTCHANGE, CURRENTPASS, OLDPASS)
VALUES(curUserID, userDateCreated, SYSDATE, newUserPwd, oldUserPwd)
END;
END IF;
I am getting this error:
Error(30,7): PL/SQL: ORA-00933: SQL command not properly ended
Error(31,7): PLS-00103: Encountered the symbol "IF" when expecting one of the following: ; <an identifier> <a double-quoted delimited-identifier>
Perhaps it's just a minor error that I am missing?

PL/SQL Syntax Error for IF THEN ELSE
Correct syntax is:
IF condition THEN
statements
END IF;
Your code is effectively:
IF condition THEN
BEGIN
statements
END;
Corrected
IF NOT EXISTS
(
SELECT ISOUID
FROM OLDUSERPASSWORDS
WHERE ISOUID=curIsoUserUID
)
THEN
BEGIN <------------ Error is NOT here
INSERT INTO OLDUSERPASSWORDS(ISOUID, DATECREATE, DATELASTCHANGE, CURRENTPASS, OLDPASS)
VALUES(curUserID, userDateCreated, SYSDATE, newUserPwd, oldUserPwd);
END;
END IF;

You are getting your first error because you need to add a THEN clause before the BEGIN block to make the syntax of the ID statement complete and correct.
You are getting the second error:
Error(30,7): PL/SQL: ORA-00933: SQL command not properly ended
because you have not completed your INSERT statement. Terminate it with a semicolon and that should fix that error.
IF NOT EXISTS
(
SELECT ISOUID
FROM OLDUSERPASSWORDS
WHERE ISOUID=curIsoUserUID
)
THEN
BEGIN
INSERT INTO OLDUSERPASSWORDS(ISOUID, DATECREATE, DATELASTCHANGE, CURRENTPASS, OLDPASS)
VALUES(curUserID, userDateCreated, SYSDATE, newUserPwd, oldUserPwd);
END;
END IF;

You forgot the THEN before the BEGIN, that goes with the IF NOT EXISTS.

Related

Oracle SQL Using Variables Not Running

This is probably an elementary question, but I'm new to Oracle SQL. I'm trying to get the SQL below to execute in Oracle SQL Developer. The error information is below the code. I've modified the code since I pasted the error message. Line 28 is the last line in the code, "END;" Line 14 column 1 refers to the word "Select".
DECLARE
v_StartDate Date := &StartDate;
v_EndDate Date := &EndDate;
SET v_StartDate := CASE &StartDate
WHEN TO_DATE('01/01/1900','MM/DD/YYYY')
THEN LAST_DAY(ADD_MONTHS(trunc(current_date),-2))
ELSE TO_DATE(&StartDate - 1) END;
SET v_EndDate := CASE &StartDate
WHEN TO_DATE('01/01/1900','MM/DD/YYYY')
THEN LAST_DAY(ADD_MONTHS(&EndDate,-1))
ELSE TO_DATE(&EndDate + 1) END;
BEGIN
Select *
From
Table
Where date_value > v_StartDate
and date_value < v_EndDate
END;
Error report -
ORA-06550: line 28, column 24:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 14, column 1:
PL/SQL: SQL Statement ignored
ORA-06550: line 28, column 27:
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 json_exists json_value json_query
json_object json_array
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
There are several things wrong with your code. I suggest you start with a small piece of code that works and then add statement by statement. That will allow you to see what is wrong and fix it. Once you have an accumulated set of syntax errors it becomes difficult to debug.
There are comments where the code has errors:
DECLARE
v_StartDate Date := &StartDate;
v_EndDate Date := &EndDate;
-- issues below:
-- 1. SET is not oracle syntax. To assign a variable, use the := operator
-- without the SET word.
-- 2. you cannot assign variables in the declaration section, unless you
-- declare and assign in the same statement. This part should go after
-- the BEGIN keyword
-- 3. Why would you use the bind variable if you already assigned it
-- above ??
SET v_StartDate := CASE &StartDate
WHEN TO_DATE('01/01/1900','MM/DD/YYYY')
THEN LAST_DAY(ADD_MONTHS(trunc(current_date),-2))
ELSE TO_DATE(&StartDate - 1) END;
SET v_EndDate := CASE &StartDate
WHEN TO_DATE('01/01/1900','MM/DD/YYYY')
THEN LAST_DAY(ADD_MONTHS(&EndDate,-1))
ELSE TO_DATE(&EndDate + 1) END;
BEGIN
-- issue below
-- you cannot just select in pl/sql, you need to SELECT INTO (if there is
-- a single row) or BULK COLLECT INTO (if you have multiple rows)
Select *
From
Table
Where date_value > v_StartDate
and date_value < v_EndDate
END;
I'm sorry to have to say it, but your code "has more errors than an early Met's game".
The key word SET belongs as a clause of the UPDATE statement. You have to UPDATE statements. If you just want to set the value of a variable, the syntax is simply
v_StartDate := some_value
And you have already set the value of v_StartDate with a parameter in you DECLARE section, so what are you trying to do with it now?
The keyword WHEN requires a comparison, but your usage
WHEN TO_DATE('01/01/1900','MM/DD/YYYY')
THEN LAST_DAY(ADD_MONTHS(trunc(current_date),-2))
Is not comparing the first arugment (TO_DATE ....) with anything.
You are trying to trunc(current_date) but 'current_date' is not defined -- and it's not a key word or reserved word. Perhaps you meant 'sysdate'.

Adding an exception (calling function) into procedure - SQL Oracle

Having some problems with adding function calling exception in the procedure. I hope it's just some syntax misstake.
The exception unauthorised is checking if the correct owner of the account deposits money in to the account. To handle that I call function get_authorisation. The code works itself without the exception. Could someone check it? Thank you!
create or replace procedure do_utt(
p_radnr in utt.radnr%type,
p_pnr in utt.pnr%type,
p_knr in utt.knr%type,
p_belopp in utt.belopp%type,
p_datum in utt.datum%type)
declare unauthorised exception;
as
begin
insert into utt(radnr, pnr, knr, belopp, datum)
values (radnr_seq.nextval, p_pnr, p_knr,
p_belopp, sysdate);
if get_authorisation(p_knr) = 0 then
raise unauthorised ;
end if;
commit;
dbms_output.put_line('Balance '|| p_knr || ' is:
'||get_saldo(p_knr));
exception
when unauthorised then
dbms_output.put_line('You are not the owner of the account!');
end;
/
Errors: PROCEDURE DO_UTT
Line/Col: 8/2 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
; is with default authid as cluster order using external
deterministic parallel_enable pipelined result_cache
accessible rewrite
The code for function:
create or replace function get_authorisation (
p_pnr in owner.pnr%type,
p_knr in owner.knr%type)
return number
as
v_authorised number;
v_no_authorised exception;
begin
select count(*)
into v_authorised
from owner
where pnr = p_pnr
and knr = p_knr;
return v_authorised;
exception
when v_no_authorised then
return 0;
end;
You should declare your own excepcion with the PRAGMA EXCEPTION_INIT clause and then throw the exception with RAISE_APPLICATION_ERROR as it's shown in this answer:
https://stackoverflow.com/a/6020523/518
Note: you should use an error code less than -20000 (in the answer -20001 is used)
--I changed the code with PRAGMA EXCEPTION_INIT --
create or replace procedure do_utt(
p_radnr in utt.radnr%type,
p_pnr in utt.pnr%type,
p_knr in utt.knr%type,
p_belopp in utt.belopp%type,
p_datum in utt.datum%type)
as
declare
unauthorised exception;
PRAGMA EXCEPTION_INIT( unauthorised, -20001 );
begin
insert into utt(radnr, pnr, knr, belopp, datum)
values (radnr_seq.nextval, p_pnr, p_knr, p_belopp, sysdate);
commit;
dbms_output.put_line('Balance '|| p_knr || ' is: '||get_saldo(p_knr));
raise_application_error (20001, 'You are not the account owner!');
exception
when get_authorisation = 0 then
dbms_output.put_line( sqlerrm );
end;
Errors: PROCEDURE DO_UTTAG
Line/Col: 8/2 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
begin function pragma procedure subtype type
current cursor delete
exists prior external language
The symbol "begin" was substituted for "DECLARE" to continue.
Line/Col: 19/26 PLS-00103: Encountered the symbol "=" when expecting one of the following:
. then or

Error creating trigger in Oracle PLSQL

I'm a student learning SQL in Oracle. I'm creating a trigger and within the trigger I'm calling a procedure update_price_history(item_id, NEW.price).
This is the statement I did,
CREATE OR REPLACE TRIGGER upd_hist_trg
BEFORE INSERT OR UPDATE OF price ON Item
FOR EACH ROW
BEGIN
IF :NEW.price != price THEN
update_price_history(item_id, NEW.price)
END IF;
END;
I thought I had everything correct but I'm getting an error message:
Error(3,2): PLS-00103: Encountered the symbol "END" when expecting one of the following: := . ( % ; The symbol ";" was substituted for "END" to continue.
I've been trying to figure out what I'm getting wrong and I'm not seeing what it is. Any help would be greatly appreciated.
You forgot the semicolon at the end
...
update_price_history(item_id, :NEW.price);
...
Try this :
CREATE OR REPLACE TRIGGER upd_hist_trg
BEFORE INSERT OR UPDATE OF price ON Item
FOR EACH ROW
DECLARE
item_id number;
BEGIN
IF :NEW.price != price THEN
update_price_history(item_id, :NEW.price);
END IF;
END;
you forgot : in front of new and a semicolon ; after procedure. By the way, i can't see any variable definition of item_id.

stored procedure in oracle 11g

create procedure p5(age1 IN number) as
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.putline('age should be high');
end if;
end p5;
Warning: Procedure created with compilation errors.
I have tried executing this and i am getting errors listed below
SQL> exec p5(20);
BEGIN p5(20); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00905: object SYSTEM.P5 is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I am still getting these errors
There are multiple problems,
create or replace procedure p3(age1 IN number) as --you dont need (3) here. Also closing braces are missing.
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.put_line('age should be high'); --you were using putline.
end if;
end p3; --your proc name is p2 but you are endin p3. Not needed. Just END will also do.
I tried running it locally and it is working fine.
create table t (age number(3));
Table T created.
create procedure p3(age1 IN number) as
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.put_line('age should be high');
end if;
end p3;
Procedure P3 compiled
set serveroutput on;
exec p3(23);
PL/SQL procedure successfully completed.
exec p3(17);
PL/SQL procedure successfully completed.
age should be high
select * from t;
AGE
23
The error is here:
dbms_output.put_line
instead of
dbms_output.putline
You missed closing parenthesis after p3(age1 IN number(3). Try following :
create procedure p3(age1 IN number) as
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.put_line('age should be high'); -- it should be put_line
end if;
end p3; --correct procedure name

Error creating Procedure

I'm getting some error with a proc I have created. The proc body is :-
CREATE OR REPLACE PROCEDURE suppress_termination_charge(v_curr_date IN DATE)
IS
TYPE suppress_term_cust_type IS RECORD (id NUMBER(11),pev NUMBER(11),piv NUMBER(11));
TYPE cur_suppress_term_cust IS REF CURSOR RETURN suppress_term_cust_type;
v_count NUMBER(4);
v_serv_item suppress_term_cust_type;
v_serv_itm_pev service_item.price_excluding_vat%TYPE;
v_serv_itm_piv service_item.price_including_vat%TYPE;
BEGIN
v_count:=0;
IF NULL=v_curr_date THEN
SELECT sysdate INTO v_curr_date FROM dual;
END IF;
OPEN cur_suppress_term_cust FOR
SELECT id AS id,price_excluding_vat AS pev,price_including_vat AS piv FROM service_items;
LOOP
FETCH cur_suppress_term_cust INTO v_serv_item;
EXIT WHEN cur_suppress_term_cust%NOTFOUND;
v_comment := 'Price changed from ('||v_serv_item.pev||', '||v_serv_item.piv||') to (0,0) (PEV, PIV) on '||v_curr_date||' for managed cease';
UPDATE service_items
SET price_including_vat=0, price_excluding_vat=0 , comments= v_comment
WHERE id = v_serv_item.id;
v_count:=v_count+1;
IF v_count=5000 THEN
COMMIT;
v_count:=0;
END IF;
END LOOP;
CLOSE cur_suppress_term_cust;
END;
/
The errors are as follows:-
SQL> SHOW ERROR;
Errors for PROCEDURE SUPPRESS_TERMINATION_CHARGE:
LINE/COL ERROR
-------- -----------------------------------------------------------------
19/30 PLS-00103: Encountered the symbol "IS" when expecting one of the
following:
. ( % ; for
23/2 PLS-00103: Encountered the symbol "FETCH" when expecting one of
the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table LONG_ double ref
char time timestamp interval date binary national character
nchar
LINE/COL ERROR
-------- -----------------------------------------------------------------
41/2 PLS-00103: Encountered the symbol "CLOSE" when expecting one of
the following:
end not pragma final instantiable order overriding static
member constructor map
I see errors but not the ones you reported:
SELECT sysdate INTO v_curr_date FROM dual;
v_curr_date is an input parameter and can't be used as a target of SELECT or FETCH.
OPEN cur_suppress_term_cust FOR...
FETCH cur_suppress_term_cust INTO v_serv_item;
EXIT WHEN cur_suppress_term_cust%NOTFOUND.
cur_suppress_term_cust is a TYPE, not a cursor variable.
v_comment := ...
v_comment is not declared.
Try fixing these issues and see if things improve.
Share and enjoy.