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

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

Related

PL/SQL Trigger: Facing bad bind variable error

I have tried everything but nothing can solve this.PLS-00049: bad bind variable 'NEW.BLOODBANKID' keep appearing. Please help me.
CREATE OR REPLACE TRIGGER count_stock
AFTER INSERT OR UPDATE ON blood
FOR EACH ROW
DECLARE
v_countblood NUMBER;
BEGIN
SELECT COUNT(bloodid) INTO v_countblood
FROM blood
WHERE bloodbankid = :NEW.bloodbankid
AND bloodtype = :NEW.bloodtype;
IF v_countblood < 0 THEN
RAISE_APPLICATION_ERROR(-20201,'There is no blood');
END IF;
END;
The error indicates that there is no column "BLOODBANKID" in the table "BLOOD".
Either it doesn't exist at all in the table or you used double quotes on the column in your CREATE TABLE statement, thus making the column case sensitive.
If for instance your CREATE TABLE statement looks like this:
create table blood("bloodbankid" number(18), ...);
Then there is a column "bloodbankid" in the table, but no column "BLOODBANKID".
When we access a row without quotes as in
select BLOODBANKID from blood where BloodBankId = 123;
then Oracle converts this internally into "BLOODBANKID". As the same applies to CREATE TABLE, the column names usually match. If you used quotes in the CREATE TABLE statement, however, then you must use the same upper/lower case with quotes in every statement:
select "bloodbankid" from blood where "bloodbankid" = 123;
So, if this is the case, I'd recommend you re-create the table with case insensitive columns.
(Apart from this your trigger doesn't make sense anyway, as has already been mentioned in the request comments.)

Why do I keep getting this error when creating a trigger?

I am creating a trigger that checks if the Majorcode column is null BEFORE INSERT OR UPDATE and if it is it enters the default value "100"
My code:
CREATE TRIGGER MUmajorcode_changes
BEFORE INSERT OR UPDATE ON M_Students
FOR EACH ROW
BEGIN
IF NEW.Majorcode = null THEN
SET NEW.Majorcode = '100';
END IF;
I get the following message after running it
Warning: Trigger created with compilation errors.
Then when I actually insert a new value with blank major code.
I receive the following message/error:
ERROR at line 1:
ORA-04098: trigger 'SCOTT.MUMAJORCODE_CHANGES' is invalid and failed re-validation.
This is how I setup the table initially:
CREATE TABLE M_Students
(
ID CHAR(3) PRIMARY KEY,
FName VARCHAR2(25),
LName VARCHAR2(25),
Status VARCHAR2(25),
Majorcode CHAR(3) REFERENCES Departments(DeptCode),
GPA NUMBER (4,2),
AdmittedDate DATE
);
Cannot figure out where I am going wrong?
When you get the message "created with compilation errors" it means your script is invalid, it has compile time errors. If your IDE does not show them you to you then if you are using sqlplus then run "show errors" if you are using an IDE run the query "select * from user_errors" (and get a new IDE). Either will show the errors in the script, they must be corrected; your script will not run until they are.
The suggestions of setting a DEFAULT value in the table definition has merit but will fail on a couple conditions. When Inserting the default will be taken when the column name is not mentioned in column list. (Note Omitting the column list is implicitly mentioning all columns so the default would not be set.) Nor would it handle update condition.
Your trigger itself can be reduced to a single statement with the coalesce function:
create or replace trigger mumajorcode_changes
before insert or update of mumajorcode
on m_students
for each row
begin
:new.majorcode := coalesce(:new.majorcode, '100');
end;
You have some syntax errors:
CREATE OR REPLACE TRIGGER MUmajorcode_changes /* better use REPLACE to avoid dropping the trigger */
BEFORE INSERT OR UPDATE
ON M_Students
FOR EACH ROW
BEGIN
IF :new.Majorcode IS NULL /* :new, with the colon (:) */
THEN
:new.Majorcode := '100'; /* :new, and no SET */
END IF;
END; /* a missing END */
Also, never check null values with =, but always use is [not] null.
There are some obvious syntax errors such as wrong assignment, missing or superfluous keywords, but you don't need to create a database trigger to handle this operation. Just modify the related columns property by
ALTER TABLE M_Students
MODIFY Majorcode DEFAULT '100' NOT NULL
Through this property, Majorcode column is already populated by the value '100' during the insertion automatically, and updating that column to NULL won't be possible because of the NOT NULL constraint.
Demo

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.

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

TRIGGER Oracle to prevent updating or inserting

I am having problems with this code below, which is a trigger used in Oracle SQL:
CREATE OR REPLACE TRIGGER TRG_TUTOR_BLOCK
BEFORE INSERT OR UPDATE ON tutors
FOR EACH ROW
DECLARE
BEGIN
IF :new.tutorName = :old.tutorName
THEN
RAISE_APPLICATION_ERROR(-20101, 'A tutor with the same name currently exists.');
ROLLBACK;
END IF;
END;
/
This trigger is used to prevent users from entering the same tutor name at different records.
After I insert two records with the same tutorname, the trigger does not block me from inserting it. Is there anyone can tell me what are the problems with this coding? Here are the sample format and insert values:
INSERT INTO tutors VALUES (tutorID, tutorName tutorPhone, tutorAddress, tutorRoom, loginID);
INSERT INTO tutors VALUES ('13SAS01273', 'Tian Wei Hao', '019-8611123','No91, Jalan Wangsa Mega 2, 53100 KL', 'A302', 'TianWH');
Trigger in Kamil's example will throw ORA-04091, you can see this with your own eyes here. ROLLBACK in a trigger is unnecessary, it runs implicitly when a trigger makes a statement to fail.
You can prohibit any DML on table by altering it with read only clause:
alter table tutors read only;
At last, integrity should be declarated with integrity constraints and not with triggers.
Good luck!
You don't need a trigger for this in Oracle.
You can do it with an "unique index" on the tutorName column (see http://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm#i1106547).
Note: about your trigger, it fails on checking for another record with the same tutorName because it's not scanning the tutors table for another record with the same tutorName, it's just comparing the tutorName values of the row you are creating (in this case, old.tutorName is just NULL, because the row doesn't exist yet).
Check the case in yours trigger body
IF :new.tutorName = :old.tutorName
It returns true only if 'tutorName' value is the same in new and old record. When you'll trying to updat some value you'll get
IF 'someTutorName' = 'someTutorName'
which will return TRUE.
Inserting row cannot fire this rule because you're trying to compare something like that:
'someTutorName' = NULL
This case always returns FALSE.
Try to use something like that
CREATE OR REPLACE TRIGGER TRG_TUTOR_BLOCK
BEFORE INSERT OR UPDATE ON tutors
FOR EACH ROW
DECLARE
rowsCount INTEGER;
BEGIN
SELECT COUNT(*) FROM tutors WHERE tutorName is :new.tutorName INTO rowsCount;
IF rowsCount > 0
THEN
RAISE_APPLICATION_ERROR(-20101, 'A tutor with the same name currently exists.');
ROLLBACK;
END IF;
END;
/
But the best solution is the one mentioned by friol - use unique index by executing SQL like this
ALTER TABLE tutors
ADD CONSTRAINT UNIQUE_TUTOR_NAME UNIQUE (tutorName);
If you wanna completely ignore recording a row to a table you can follow these steps
rename table to something else and create a view with the same name and create an instead of trigger.
create table usermessages (id number(10) not null)
GO
alter table usermessages rename to xusermessages
GO
create or replace view usermessages as (select * from xusermessages)
GO
create or replace trigger usermessages_instead_of_trg
instead of insert or update on usermessages
for each row
begin
Null ;
end ;
GO
insert into usermessages(123)
Live test available here below
http://sqlfiddle.com/#!4/ad6bc/2

Resources