SQL Trigger error (ORA-00942: table or view does not exist) - oracle

I create this sql trigger:
CREATE OR REPLACE TRIGGER create_event_from_task BEFORE INSERT ON llx_projet_task
BEGIN
INSERT INTO llx_actioncomm (priority, fulldayevent, location, label, fk_element, elementtype, fk_project, datep, datef, percentage, note)
SELECT 0, 0, '', 'prova', id, 'project_task', fk_project, date_start, date_end, progress, description
FROM inserted;
END;
/
But when I execute says this errors:
Errors: TRIGGER CREATE_EVENT_FROM_TASK
Line/Col: 2/2 PL/SQL: SQL Statement ignored
Line/Col: 4/7 PL/SQL: ORA-00942: table or view does not exist
Can anyone help me?

I think it got this error because of the select query inside the trigger. Check if the user which called the trigger has the grant to execute select from the "inserted" table.
https://www.tekstream.com/resource-center/ora-00942-table-or-view-does-not-exist/
Ora-00942 means you are attempting to execute an SQL statement that references a table or view which does not exist. There are several possible causes for the “table or view does not exist” error, including:
1) Referencing a table or view that does not exist
2) Using an unauthorized synonym
3) Using an expression of view where a table is required
4) Attempting to use a table without proper permission or privilege

inserted is part of SQL Server. I think you want:
BEGIN
INSERT INTO llx_actioncomm (priority, fulldayevent, location, label, fk_element, elementtype, fk_project, datep, datef, percentage, note)
SELECT 0, 0, '', 'prova', :new.id, 'project_task',
:new.fk_project, :new.date_start, :new.date_end,
:new.progress, :new.description
FROM dual;
END;

Related

I'm trying to write a PL/SQL Procedure that cancels a reservation. I keep getting an identifier error on a column name

create or replace procedure Cancel_Reservation(resid in number)
as
Begin
Update Reservation
set Res_cancel = 'yes'
where resid = reserve_id;
end;
Here is a picture of the table I'm working with
My goal is to cancel a reservation by inputting a reserve_id and changing res_cancel to 'yes' from 'no'. My plan is to not delete the reservation.
Here are the errors:
Error(3,1): PL/SQL: SQL Statement ignored
Error(4,5): PL/SQL: ORA-00904: "RES_CANCEL": invalid identifier
Whoever created a table using columns with mixed case ... well, didn't think twice. In Oracle, never do that; causes nothing but problems. I suggest you rename the column with
alter table reservation rename column "Res_cancel" to res_cancel;
Because, as it is, you must reference that table using double quotes and exactly match letter case. So:
Update Reservation
set "Res_cancel" = 'yes' --> this

EFCore error:- ORA-00904: "m"."Id": invalid identifier

I am working with an application using asp.net core 2.2 and efcore database first approach with Oracle database. I am using Oracle.EntityFrameworkCore (2.19.60) nuget package, successfully mapped db model, but when I try to fetch data from DBContext , getting error
ORA-00904: "m"."Id": invalid identifier
Oracle Database version: Oracle Database 12c Standard Edition Release 12.2.0.1.0 - 64bit Production
code:
var fetched = await myDatabaseContext.MyTableVersions.ToListAsync();
LinQ is generating following query :
SELECT "m"."Id", "m"."MAJORVERSION" FROM "MyTableVersions" "m"
Since It's not a correct syntax for PL/Sql query so getting error ORA-00904: "m"."Id": invalid identifier.
Is there any way to fix this error? Thanks.
I have searched on the web and found
Bug 30352492 - EFCORE: ORA-00904: INVALID IDENTIFIER
but that issue is related to schema.
The query is perfectly valid (db<>fiddle here), assuming your table looks something like
CREATE TABLE "MyTableVersions"
("Id" NUMBER,
MAJORVERSION NUMBER)
However, I suspect your table looks like
CREATE TABLE "MyTableVersions"
(ID NUMBER,
MAJORVERSION NUMBER)
I don't know what the class looks like that you're trying to fetch into, but I suspect it has a field named Id. If you can change the name of that field to ID (in other words, so that its capitalization matches the capitalization of the related database column) you might find it works then.
Double quotes are something you should avoid in Oracle world.
Your query:
SELECT "m"."Id", "m"."MAJORVERSION" FROM "MyTableVersions" "m"
means that column name is exactly Id (capital I followed by d). Only if that's really so, you should use double quotes. Otherwise, simply remove them:
SELECT m.id, m.MAJORVERSION FROM MyTableVersions m
The same goes for the table name - if it was created using mixed case (and you can't do that without double quotes), you'll have to use double quotes and exactly same letter case always. Otherwise, don't use them.
Oracle is case-insensitive regarding object and column names and are UPPERCASE by default:
SQL> create table test (id number);
Table created.
SQL> desc test
Name Null? Type
----------------------------------------- -------- -----------------
ID NUMBER
SQL> select table_name, column_name
2 from user_tab_columns
3 where table_name = 'TEST';
TABLE_NAME COLUMN_NAME
------------------------------ ------------------------------
TEST ID
SQL> insert into test (id) values (1);
1 row created.
SQL>
But, if you use mixed case with double quotes, then use them always:
SQL> create table "teST" ("Id" number);
Table created.
SQL> insert into test (id) values (1);
insert into test (id) values (1)
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> insert into "test" (id) values (1);
insert into "test" (id) values (1)
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> insert into "teST" (id) values (1);
insert into "teST" (id) values (1)
*
ERROR at line 1:
ORA-00904: "ID": invalid identifier
SQL> insert into "teST" ("Id") values (1);
1 row created.
SQL>
So: first make sure what table and column names really are, then use them appropriately.
To avoid problems with case-sensitivity, add the option to eliminate EF to add quotes to the generated queries. For instance, if you are using Devart Oracle provider, this is done in the following way:
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpDbContextOptions>(options =>
{
var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.Workarounds.DisableQuoting = true;
...
}
}
If you are using official provider - just inherit from DbCommandInterceptor (do quotes replacement with empty character in dbCommand in ...Executing methods), add this interceptor to DbContextOptionsBuilder instance.

Adding through view trigger error

I have this view and the instead of trigger for inserting through view
CREATE VIEW LivrareNoua AS
Select numef,numec,numep,cantitate
From Furnizori F, Componente C, Proiecte P, Livrari L
Where F.idf = L.idf AND C.idc = L.idc AND P.idp = L.idp;
I don't know what the problem might be
create or replace TRIGGER triggerinstead
INSTEAD OF INSERT ON LivrareNoua
FOR EACH ROW
BEGIN
INSERT INTO Furnizori(idf,numef) VALUES('&idf',:new.numef);
INSERT INTO Componete(idc,numec) VALUES('&idc',:new.numec);
INSERT INTO Proiecte(idp,numep) VALUES('&idp',:new.numep);
INSERT INTO Livrari(idf,idc,idp,cantitate) VALUES('&idf','&idc','&idp',:new.cantitate);
END;
ERRORS:
3 13 PL/SQL: ORA-00942: table or view does not exist
3 1 PL/SQL: SQL Statement ignored
make sure that the development schema is the same as the schema of the view

How can I solve the mutating table error here?

set serveroutput on;
CREATE OR REPLACE TRIGGER hw3
BEFORE DELETE on ENROLLS
for EACH ROW
ENABLE
DECLARE
v_sid number;
v_term varchar2(20);
v_sectno number;
v_COMPNAME varchar2(20);
v_points number;
BEGIN
select :old.SID,:old.TERM,:old.SECTNO into v_sid,v_term,v_sectno from enrolls;
select COMPNAME,points into v_compname,v_points from scores
where scores.sid=v_sid and scores.term=v_term and scores.sectno=v_sectno;
INSERT into DELETED_SCORES (SID,TERM,SECTNO,compname,points)
values (v_sid,v_term,v_sectno,v_compname,v_points);
DELETE FROM SCORES
WHERE SID=V_SID AND TERM=V_TERM AND SECTNO=V_SECTNO;
END;
/
There are two table, which is enrolls and scores. And SCORES table has a combined foreign keys including SID,TERM,AND SECTNO referring to table ENROLLS.
The trigger is now successfully compiled, but there is a problem shown as below:
Error starting at line : 24 in command -
DELETE FROM enrolls
WHERE SID=1111 and term='F12' and sectno=1031
Error report -
SQL Error: ORA-04091: table C16_HE_JIEL.ENROLLS is mutating, trigger/function may not see it
ORA-06512: at "C16_HE_JIEL.HW3", line 8
ORA-04088: error during execution of trigger 'C16_HE_JIEL.HW3'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
never use before trigger for DML. In some situations before trigger can be fired more than once for the same row. It might happen in situations when your session is blocked by other session updating/deleting the same row.
Use compound trigger and apply DML changes in after trigger session.
Then you can be sure, that data you see are really correct.
The mutating table error means that Oracle can not quarantee that what you're doing is deterministic.
This select:
select :old.SID,:old.TERM,:old.SECTNO into v_sid,v_term,v_sectno from enrolls;
Is invalid for two reasons:
you can't select from the table where the trigger fired on
you are selecting all rows from that table, not just one.
The select isn't needed anyway, you can simply use the values in the OLD record directly (and remove the invalid select ... from enrolls)
select COMPNAME,points
into v_compname,v_points
from scores
where scores.sid = :old.SID
and scores.term = :old.TERM
and scores.sectno = :old.SECTNO;
The above statement requires that the combination of (sid, term, secno) is unique in the table scores. If that is not the case and you need to insert multiple rows with into deleted_scores you need to use an INSERT based on a select.
Which then removes the need for variables completely. So the whole trigger can be simplified to:
CREATE OR REPLACE TRIGGER hw3
BEFORE DELETE on ENROLLS
for EACH ROW
BEGIN
INSERT into DELETED_SCORES (SID,TERM,SECTNO,compname,points)
select sid, term, sectno, compname, points
from scores
where scores.sid = :old.SID
and scores.term = :old.TERM
and scores.sectno = :old.SECTNO;
DELETE FROM SCORES
WHERE SID = :OLD.sid
AND TERM = :OLD.term
AND SECTNO = :OLD.sectno;
END;
/
More details about the "table is mutating" restriction can be found in the manual:
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS759

SQL Error: ORA-04091: table is mutating while updating by a trigger

i am new to oracle , i am developing a hospital management system , i have this table to store patients :
create table patients(
p_id number not null primary key,
p_fullname full_name_ty,
p_gender char,
de_no number,
p_entry date ,
Diagnosis varchar2(25),
p_exit date,
constraint pdf foreign key (de_no) references department(dep_no)
);
where p_entry is the date when a patient enters the hospital , i made a trigger that calculates the residency time in the hospital on after the upadate of the (p_exit) date for the patient (setting this date means that the patient has left the hospital) , the trigger will simply calculate the difference between the two dates , and print it , here is the code of the trigger :
create or replace
trigger period_trig before update of p_exit on patients for each row
DECLARE
period Number(3);
enterr DATE;
exitt DATE;
BEGIN
enterr := :old.P_ENTRY;
exitt:= :NEW.P_EXIT;
Period :=exitt-enterr;
DBMS_OUTPUT.PUT_LINE('Duration:'||period);
update patients SET RESIDENCY= Period where P_ID = :old.P_ID;
end period_trig
put when i test the trigger and use an update statement like this :
update patients set p_exit = to_date('01/02/2001','dd/mm/yyyy') where p_id = 2;
and run it i get this error :
Error starting at line 1 in command:
update patients set p_exit = to_date('01/02/2001','dd/mm/yyyy') where p_id = 2
Error report:
SQL Error: ORA-04091: table SEM.PATIENTS is mutating, trigger/function may not see it
ORA-06512: at "SEM.UPDATEPAT", line 5
ORA-06512: at "SEM.PERIOD_TRIG", line 10
ORA-04088: error during execution of trigger 'SEM.PERIOD_TRIG'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
Error starting at line 1 in command:
update patients set p_exit = to_date('01/02/2001','dd/mm/yyyy') where p_id = 2
Error report:
SQL Error: ORA-04091: table SEM.PATIENTS is mutating, trigger/function may not see it
ORA-06512: at "SEM.PERIOD_TRIG", line 11
ORA-04088: error during execution of trigger 'SEM.PERIOD_TRIG'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
can anyone tell me how to fix it ? and thanks so much ..
You are modifying the very same table in the trigger that is currently being modified. As they error tells you:
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
There is actually no need in updating the table again, you can simply use a virtual column directly on the table that makes the entire trigger redundant:
CREATE TABLE patients(
p_id NUMBER NOT NULL PRIMARY KEY,
p_fullname VARCHAR2(255),
p_gender CHAR(1),
de_no NUMBER,
p_entry DATE,
Diagnosis VARCHAR2(25),
p_exit DATE,
RESIDENCY NUMBER GENERATED ALWAYS AS (p_exit-p_entry)
);
insert into patients (p_id, p_fullname, p_gender, de_no, p_entry, diagnosis) values (1, 'GVENZL', 'M', 1234, SYSDATE-1, 'healthy' );
commit;
select p_fullname, residency from patients;
update patients set p_exit = sysdate;
commit;
select p_fullname, residency from patients;

Resources