I have problem compile trigger using merge - oracle

can You explain me, where I did mistake in my code ?
CREATE OR REPLACE TRIGGER mda_01.tr_01_t01003 AFTER
INSERT ON mda_01.t01004_bo_semafor_log
FOR EACH ROW
DECLARE
PRAGMA autonomous_transaction;
BEGIN
IF inserting THEN
MERGE INTO mda_01.t01003_bo_semafor a
USING (
SELECT
:new.semafor_name,
:new.semafor_ts,
:new.semafor_status,
:new.semafor_desc
FROM
dual
) b
ON ( b.semafor_name = a.semafor_name )
WHEN MATCHED THEN UPDATE
SET a.semafor_ts = b.semafor_ts,
a.semafor_status = b.semafor_status
WHEN NOT MATCHED THEN
INSERT (
semafor_name,
semafor_ts,
semafor_status,
semafor_desc )
VALUES
( b.semafor_name,
b.semafor_ts,
b.semafor_status,
b.semafor_desc );
END IF;
COMMIT;
END;
/
I get an error:
LINE/COL ERROR
5/6 PL/SQL: SQL Statement ignored
16/8 PL/SQL: ORA-00904: "B"."SEMAFOR_NAME": invalid identifier
Errors: check compiler log
Thanks, Paul

I solved the problem.
I added aliases, and trigger was compiled with no errors:
SELECT
:new.semafor_name as semafor_name,
:new.semafor_ts as semafor_ts,
:new.semafor_status as semafor_status,
:new.semafor_desc as semafor_desc
FROM
dual

Related

Getting error while creating Trigger in Oracle

I have created a trigger below but its giving me error as
3/1 PLS-00428: an INTO clause is expected in this SELECT statement 5/1 PL/SQL: SQL Statement ignored 6/28 PL/SQL: ORA-00984: column not allowed here Errors: check compiler log
Below is my code
create or replace TRIGGER "APP_WFM"."TRG_INS_NE_SF_SITE_INSTANCE"
BEFORE INSERT OR UPDATE ON NE_SITE_INSTANCE
FOR EACH ROW
BEGIN
select rf_siteid, sap_id, site_name from SF_NE_DETAILS;
INSERT INTO NE_SITE_INSTANCE (rf_siteid, sap_id, sitename)
VALUES (rf_siteid, sap_id, site_name);
END;
What can I try to resolve this?
Based on your previous question, you want to use the :NEW record to get the inserted values and then insert into the second table that you are copying into:
CREATE TRIGGER APP_WFM.TRG_INS_NE_SF_SITE_INSTANCE
BEFORE INSERT OR UPDATE ON NE_SITE_INSTANCE -- Table being copied from
FOR EACH ROW
BEGIN
INSERT INTO SF_NE_DETAILS ( -- Table being copied to
rf_siteid,
sap_id,
site_name
) VALUES (
:NEW.rf_siteid,
:NEW.sap_id,
:NEW.site_name
);
END;
/
db<>fiddle here

Why i'm getting "PLS-00302: component 'TABLE_NAME' must be declared"?

I'm trying to create a fairly simple stored procedure in Oracle 12.2 DB:
create or replace procedure list_tables (
p_src_schema varchar2
)
as
l_src_schema varchar2(30) := upper(p_src_schema);
begin
for x in (select table_name name from all_tables where owner = l_src_schema
and not regexp_like(table_name, '(AAA|BKP_|LOG_|TMP_|TEST|XX).*')
order by table_name)
loop
dbms_output.put_line(x.table_name);
end loop;
end;
/
show errors
and i'm getting the following error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
11/9 PL/SQL: Statement ignored
11/32 PLS-00302: component 'TABLE_NAME' must be declared
the error occurs in the following line: dbms_output.put_line(x.table_name);
Question: what do I do wrong? I must be overseeing something very obvious...
UPDATE: the alias name has been "added" after pressing <TAB> by the dBeaver autocompletion - and I didn't notice it.
;)
Because you used column alias:
select table_name name from
----
which means that you should have used
dbms_output.put_line(x.name);
instead.

I am getting compilation error in creating Oracle Proceedure

iam creating a proceedure it is giving compilation error
of right paranthesis missing
Line # = 9 Column # = 21 Error Text = PL/SQL: ORA-00907: missing right
parenthesis
CREATE OR REPLACE PROCEDURE user1.trns (XMLDATA XMLTYPE)
IS
BEGIN
UPDATE table1 SET T$fld1 = 1, T$dat1 = TO_DATE(TO_CHAR(sysdate,'DD/MM/YYYY'),'DD/MM/YYYY')
WHERE table1.T$docn IN
(
SELECT DISTINCT docn
FROM XMLTABLE
('/DataSet/Document/DocumentRow'
PASSING XMLDATA COLUMNS
docn PATH '#DOCN'
)
);
COMMIT;
END trns;
/
what am i doing wrong here
You are missing data type here as below please provide data type for XMLTABLE column:
here i have written varchar2(30)
CREATE OR REPLACE PROCEDURE user1.trns (XMLDATA XMLTYPE)
IS
BEGIN
UPDATE table1 SET T$fld1 = 1, T$dat1 = TO_DATE(TO_CHAR(sysdate,'DD/MM/YYYY'),'DD/MM/YYYY')
WHERE table1.T$docn IN
(
SELECT DISTINCT docn
FROM XMLTABLE
('/DataSet/Document/DocumentRow'
PASSING XMLDATA COLUMNS
docn varchar2(30) PATH '#DOCN'
)
);
COMMIT;
END trns;
Here is my output

Oracle Pl/Sql Procedure Help Merge

I have a procedure created and i am using a merge query inside it.
when i compile the procedure, there is no error and when i try to execute it giving me
below error. Can someone help in solving this.
Code:
CREATE OR REPLACE PROCEDURE DEVICE.Check1
IS
BEGIN
MERGE INTO DEVICE.APP_C_CATEGORY A
USING (SELECT market_segment_id,
market_segment_name,
UPDATE_USER,
UPDATE_DATE
FROM CUST_INTEL.MSE_MARKET_SEGMENT_MASTER#SOURCE_CUST_INTEL.ITG.TI.COM
WHERE market_segment_id NOT IN ('120', '130', '100')) B
ON (A.APP_CATEGORY_ID = B.market_segment_id
AND A.APP_CATEGORY_NAME = B.market_segment_name)
WHEN MATCHED
THEN
UPDATE SET A.DESCRIPTION = B.market_segment_name,
A.PARENT_APP_AREA_ID = NULL,
A.RECORD_CHANGED_BY = B.UPDATE_USER,
A.RECORD_CHANGE_DATE = B.UPDATE_DATE
WHEN NOT MATCHED
THEN
INSERT (A.APP_CATEGORY_NAME,
A.DESCRIPTION,
A.TYPE,
A.PARENT_APP_AREA_ID,
A.RECORD_CHANGED_BY,
A.RECORD_CHANGE_DATE)
VALUES (B.market_segment_name,
B.market_segment_name,
1,
NULL,
B.UPDATE_USER,
B.UPDATE_DATE);
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE (SQLERRM);
DBMS_OUTPUT.PUT_LINE (SQLCODE);
END;
/
Error: when i am executing
BEGIN
DEVICE.CHECK1;
COMMIT;
END;
the following errors occur:
ORA-06550: line 2, column 10:
PLS-00302: component 'CHECK1' must be declared
ORA-06550: line 2, column 3:
PL/SQL: Statement ignored
This is nothing to do with the merge, it isn't getting as far as actually executing your procedure. From the create procedure statement you have a schema called DEVICE. Since the error message is only complaining about CHECK1, not DEVICE.CHECK1, you also appear to have a package called DEVICE. Your anonymous block is trying to find a procedure called CHECK1 within that package, not at schema level.
If you are connected as the device schema owner (user) when you execute this, just remove the schema prefix:
BEGIN
CHECK1;
COMMIT;
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