Creating a Trigger Before Update [duplicate] - oracle

i'm trying to run this simple anonymous block on TOAD but i get the following error. Can someone help me?
That's the code:
BEGIN
FOR REC_CONF IN
(
SELECT DISTINCT CONF.SCHEMA, CONF.TABELLA, CONF.CAMPO, CONF.TIPO_CAMPO, CONF.LUNG_CAMPO,
CONF.CAMPO_ACCESSO
FROM EDWH.EDWH_GDPR_CONFIG CONF
WHERE UPPER(FLAG_CANC) = 'Y'
AND UPPER(SCHEMA) = UPPER('EDWH')
ORDER BY CONF.TABELLA, CONF.CAMPO ASC
)
LOOP
DBMS_OUTPUT.PUT_LINE (REC_CONF.TABELLA);
END LOOP;
END;
This should loop into the EDWH.EDWH_GDPR_CONFIG and print out the attribute into the dbms_output.put_line.
This is the error i get:
[Error] Execution (10: 8): ORA-06550: row 10, column 8:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<identifier between quotes>
<a bind variable> << continue close current delete fetch
lock insert open rollback savepoint set sql execute commit
forall merge pipe purge)

I believe that you ran the script in a wrong manner. It is a PL/SQL script, so run it either by pressing the F9 key, or pushing the "Execute as script" button in TOAD's toolbar.
If cursor was placed somewhere in this code (e.g. on the BEGIN) and you pressed <Ctrl + Enter>, then you'll get
ORA-06550: line 12, column 4:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
( begin case declare 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
So - run it as script.

Try to add '/' after last "END;"
END LOOP;
END;
/
and execute again

Related

how to use refcursor?

I am trying to run an explain plan using a wrapper, but getting compilation error. Can you please help troubleshoot this error.
set serveroutput on
var my_cur refcursor;
begin
:my_cur:=sys.run_xplan('select * from hcc.Tab_A');
end;
/print my_cur
Error:
begin
:my_cur:=sys.run_xplan('select * from hcc.Tab_A');
end;
/print my_cur
Error report -
ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "/" 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
<an identifier> <a double-quoted delimited-identifier>
<a bind variable> << continue close current delete fetch lock
insert open rollback savepoint set sql execute commit forall
merge pipe purge
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.

How do I handle the exception in this pl/sql for loop correctly?

First off, I'm a relative newbie to PL/SQL so I might be missing something trivial.
Here is a snippet of code that I'm having issues with running -
FOR indx IN 1 .. arr.COUNT
LOOP
SELECT COUNT(*), ca.cities
INTO tmp_count, affected_cities
FROM PDB.utilities ca
WHERE (ca.app_city_id = cityid
AND ca.app_plumbing_id = arr(indx))
AND( BITAND(options1,2) = 2
OR BITAND(options1,1) = 1)
GROUP BY ca.cities;
IF tmp_count >=0 THEN
-- We have an affected app so collect metrics
IF plumbings(indx_mv) ='0Ci30000000GsBN' THEN
count_wrigley:= count_wrigley+tmp_count;
END IF;
counter:= counter+tmp_count; --overall count.
tmp_count:=0;
affected_cities:=null;
END IF;
EXCEPTION -- error thrown here !
WHEN NO_DATA_FOUND THEN
CONTINUE;
END;
END LOOP; -- Error thrown here too.
And here is my error trace -
Error report:
ORA-06550: line 64, column 13:
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
ORA-06550: line 68, column 11:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
;
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
It's worth noting that the block fails only with the exception handling and compiles successfully otherwise. So my guess is I'm doing something wrong there?
Any help would be greatly appreciated! Thanks
EXCEPTION aligns with BEGIN ... END blocks. There is no BEGIN inside your loop, so there should be no exception either.
It seems the purpose of the exception is to suppress NO_DATA_FOUND errors inside the loop. So to fix this error you need to put a BEGIN / END block in the loop too. (Ah, you have an END just no BEGIN - your code would hurl with the EXCEPTION block).
FOR indx IN 1 .. arr.COUNT
LOOP
BEGIN
SELECT COUNT(*), ca.cities
INTO tmp_count, affected_cities
FROM PDB.utilities ca
....
EXCEPTION
WHEN NO_DATA_FOUND THEN
CONTINUE;
END;
END LOOP;

TRIGGER tU_DEDPARM_OEFix compiled Warning: execution completed with warning

I am running below create trigger query in Oracle :-
SQL>CREATE OR REPLACE TRIGGER tU_DEDPARM_OEFix
AFTER INSERT OR UPDATE ON DED_PARM FOR EACH ROW
BEGIN
UPDATE DED_PARM SET
DED_PARM.OVRD_DED_AM = COALESCE(i.OVRD_DED_AM,0.00),
DED_PARM.OVRD_DED_PC = COALESCE(i.OVRD_DED_PC,0.00000)
FROM DED_PARM AS d INNER JOIN inserted AS i
ON (i.INTERNAL_EMPL_ID=d.INTERNAL_EMPL_ID AND
i.APPOINTMENT_ID=d.APPOINTMENT_ID AND
i.DEDTYP_CD=d.DEDTYP_CD AND
i.EFFECTIVE_DT=d.EFFECTIVE_DT)
END; /
After execution of the query i faced below error :-
TRIGGER tU_DEDPARM_OEFix compiled Warning: execution completed with
warning.
Then i ran the below query to see the compilation errors
SQL>show error Errors for TRIGGER TU_DEDPARM_OEFIX;
Below are the errors :-
5/3 PL/SQL: ORA-00933: SQL command not properly ended 2/3
PL/SQL: SQL Statement ignored 10/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
Can somebody help me to prepare correct query. FYI- The trigger is created but it is created with compilation errors that i showed above.
Thanks!!
This is not valid syntax for update in Oracle. I'm not sure what inserted do in MSSQL (:new or :old) but I think this is Oracle statement for that trigger
CREATE OR REPLACE TRIGGER tU_DEDPARM_OEFix
AFTER INSERT OR UPDATE ON DED_PARM FOR EACH ROW
BEGIN
update DED_PARM d SET
OVRD_DED_AM = COALESCE(:new.OVRD_DED_AM,0),
OVRD_DED_PC = COALESCE(:new.OVRD_DED_PC,0)
where INTERNAL_EMPL_ID = :new.INTERNAL_EMPL_ID and
APPOINTMENT_ID = :new.APPOINTMENT_ID and
DEDTYP_CD = :new.DEDTYP_CD and
EFFECTIVE_DT = :new.EFFECTIVE_DT;
END;

Warning: Package Body created with compilation errors

Please check my package and procedures.
My package:
create or replace package transaction1 as
procedure enter_transaction(acc number, kind varchar2, amount number);
procedure apply_transaction;
end;
/
This is my body:
create or replace package body transaction1 as
procedure enter_transaction(acc in number, kind in varchar2, amount in number)
is
begin
end;
procedure apply_transaction
is
begin
end;
end;
/
What is the warning? Why?
If you see a warning: Errors: check compiler log
then run show errors command and you will see the error log :
7/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
14/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
In case of your package body Oracle complains because BEGIN END blocks don't contain any commands.
In Oracle the BEGIN-END block must contain at least one command. Could be NULL, if you don't want to run anything (and don't forget to place a semicolon after NULL command):
PROCEDURE ......
IS
BEGIN
NULL;
END;

Oracle stored procedure

Can anyone help me by telling me what is wrong with the following syntax? I am just trying to run a simple SELECT statement in a stored procedure.
CREATE OR REPLACE PROCEDURE PVSSRDB.GETBATTERYSTATUSFORALLLOGGERS
(
p_Logger OUT e.comment_,
p_tStamp OUT h.ts,
p_Val OUT h.value_number
)
AS
BEGIN
select
e.comment_,
max(h.ts),
avg(h.value_number)
INTO
p_Logger,
p_tStamp,
p_Val
FROM
PVSSRDB.ELEMENTS e inner join PVSSRDB.DB15MINHISTORY_00100009 h on h.element_id =e.element_id
WHERE
e.element_name like 'System1:H%.BatteryCondition'
GROUP BY
e.comment_
ORDER by 2 asc
END GETBATTERYSTATUSFORALLLOGGERS;
I keep getting the same 3 errors stating:
Error(9,3): PL/SQL: SQL Statement Ignored
Error(23,18): PL/SQL: ORA_00933: SQL command not properly ended
Error(24,34): 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
You're missing a semicolon after the ORDER BY 2 asc
Also you need to declare your OUT parameters properly.
CREATE OR REPLACE
PROCEDURE PVSSRDB.GETBATTERYSTATUSFORALLLOGGERS (
p_Logger OUT PVSSRDB.ELEMENTS.comment_%TYPE,
p_tStamp OUT PVSSRDB.DB15MINHISTORY_00100009.ts%TYPE,
p_Val OUT PVSSRDB.DB15MINHISTORY_00100009.value_number%TYPE
)
AS
BEGIN
select e.comment_,
max(h.ts),
avg(h.value_number)
INTO p_Logger,
p_tStamp,
p_Val
FROM PVSSRDB.ELEMENTS e
inner join PVSSRDB.DB15MINHISTORY_00100009 h on (h.element_id =e.element_id)
WHERE e.element_name like 'System1:H%.BatteryCondition'
GROUP BY e.comment_
ORDER by 2 asc;
END GETBATTERYSTATUSFORALLLOGGERS;

Resources