Transaction procedure [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
It should make a bank transaction. So i have a table called "transaction" and also an "account" table and a "withdraw" and "deposit". The procedure should create a row in the transaction table. It should call my function which looks if the person is certified. If the user isnt error should show of course. If the transaction has been made the new "saldo" (which is in the account table) of both accounts should be printed. This is what i have so far:
create or replace procedure do_transaction(
p_rownr in transaction.rownr%type,
p_pnr in transaction.pnr%type,
p_knr in transaction.knr%type,
p_date in transaction."date"%type)
as
not_certified exception;
begin
insert into transaction(rownr,pnr,knr,"date")
values(p_rownr,p_pnr,p_knr,p_date);
if user <> get_certification(p_pnr,p_knr) then
raise not_certified;
end if;
dbms_output.put_line('Pnr: '||''||p_pnr||''||'Current saldo: '||''||get_saldo(p_knr)); /*I also have a function which gets the saldo from the matching knr*/
commit;
exception
when not_certified then
raise_application_error(-20007,'Not certified!');
end;
The get certification function:
create or replace function get_certification(
p_pnr in bankcust.pnr%type,
p_knr in account.knr%type)
return varchar2
as
v_certification bankcust.pnr%type;
begin
select count(*)
into v_certification
from bankcust,account
where pnr = p_pnr
and knr = p_knr;
return v_certification;
exception
when no_data_found then
return -1;
end;
Any suggestions?

I am going to take a guess here as you did not actually state your problem. But that issue is your do_transaction procedure always results in the not certified exception. This is the result of the statement
if user <> get_certification(p_pnr,p_knr) then
This will always evaluate true. It compares the character representation of the result count function from get_certification to the current user (unless the user is presumably '1') thus always resulting in "raise not_certified;" being executed. Additionally, while not actually an exception but falls into the category of not doing unnecessary work; validate certification before inserting into transaction. So:
create or replace procedure do_transaction(
p_rownr in transaction.rownr%type,
p_pnr in transaction.pnr%type,
p_knr in transaction.knr%type,
p_date in transaction."date"%type)
as
not_certified exception;
begin
if get_certification(p_pnr,p_knr) = 0 then
raise not_certified;
end if;
insert into transaction(rownr,pnr,knr,"date")
values(p_rownr,p_pnr,p_knr,p_date);
dbms_output.put_line('Pnr: '||''||p_pnr||''||'Current saldo: '||''||get_saldo(p_knr)); /*I also have a function which gets the saldo from the matching knr*/
commit;
exception
when not_certified then
raise_application_error(-20007,'Not certified!');
end do_transaction;

Related

Updating the record of same table when new record is inserted or updated in oracle

I am new to learning Oracle. I have a task in which I need to update value of any previous record if new record contains its reference.
Table structure is as below :
Review_Table
(review_id number pk,
review_name varchar2,
previous_review number null,
followup_review number null
)
Here previous_review and followup_review columns are objects of same table i.e Review_table.
Now consider we have two records in Review_table A and B, A does not have any previous or followup review. When user creates/updates the record B and he selects record A as previous record, then we want to automatically update (via trigger) the value of A record's followup review with B's Review ID.
I have tried writing following trigger
create or replace trigger "REVIEW_T1"
AFTER insert or update on "REVIEW_TABLE"
for each row
begin
update REVIEW_TABLE
set review_follow_up_review = :new.REVIEW_ID
where REVIEW_ID = :new.REVIEW_PREVIOUS_REVIEW;
end;
But I am getting error as : REVIEW_TABLE is mutating, trigger/function may not see it ORA-06512
I have tried searching everything but was unable to find any solution for it
TL;DR: No trigger, no mutating. Do not use trigger to change another row in the same table.
I absolutely agree with #StevenFeuerstein's comment:
I also suggest not using a trigger at all. Instead, create a package that contains two procedures, one to insert into table, one to update. And within these procedures, implement the above logic. Then make sure that the only way developers and apps can modify the table is through this package (don't grant privs on the table, only execute on the package).
Take a look at the following example.
Prepare the schema:
create table reviews (
id number primary key,
name varchar2 (32),
previous number,
followup number
);
create or replace procedure createNextReview (name varchar2, lastId number := null) is
lastReview reviews%rowtype;
nextReview reviews%rowtype;
function getLastReview (lastId number) return reviews%rowtype is
begin
for ret in (
select * from reviews where id = lastId
for update
) loop return ret; end loop;
raise_application_error (-20000, 'last review does not exist');
end;
procedure insertReview (nextReview reviews%rowtype) is
begin
insert into reviews values nextReview;
exception when others then
raise_application_error (-20000, 'cannot insert next review');
end;
procedure setFollowUp (nextId number, lastId number) is
begin
update reviews set
followup = nextId
where id = lastId
;
exception when others then
raise_application_error (-20000, 'cannot update last review');
end;
begin
if lastId is not null then
lastReview := getLastReview (lastId);
end if;
nextReview.id := coalesce (lastReview.id, 0)+1;
nextReview.name := name;
nextReview.previous := lastId;
insertReview (nextReview);
if lastReview.Id is not null then
setFollowUp (nextReview.id, lastReview.Id);
end if;
exception when others then
dbms_output.put_line (
'createNextReview: '||sqlerrm||chr(10)||dbms_utility.format_error_backtrace ()
);
end;
/
Execute:
exec createNextReview ('first review')
exec createNextReview ('next review', 1)
See the outcome of work done:
select * from reviews;
ID NAME PREVIOUS FOLLOWUP
---------- ---------------- ---------- ----------
1 first review 2
2 next review 1
First you need to read about triggers, mutating table error and compound triggers: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS2005
Your trigger is AFTER UPDATE OR INSERT. Means if you run UPDATE OR INSERT statements on this table, the trigger will fire. But you are trying to update the same table again inside your trigger, which is compl. wrong.
I think you can fix this by rewriting this as a before trigger, rather than an after trigger.

sql project aid required [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to generate a trigger that if a column within a table is filled in with a specific set value e.g. i have a column named 'Volunteertype' which can only be assigned either 'staff' of 'student', if student is entered the trigger must nullify all columns relating to staff details within the volunteer table such as 'area of work', 'staffmanager' etc below is code i have tried to put together utilising the resources on the website however i'm having no luck.
CREATE TRIGGER trg_voltype
ON Volunteer
AFTER INSERT
AS
IF EXISTS (SELECT NULL FROM inserted i WHERE VolunteerType = 'student')
FOR EACH ROW BEGIN;
UPDATE v
SET Area_of_work = NULL
SET StaffManagerName = NULL
SET StaffManagerEmail = NULL
SET StaffManagerPhone = NULL
FROM Volunteer v
JOIN inserted i ON v.Volunteer_id = i.Volunteer_id
WHERE v.VolunteerType = 'student';
END;
However when this is run within the Oracle environment an error is produced ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword. when i attempt to shift the 'AFTER INSERT' to before the keyword i get an 'invalid trigger' error
Is anyone able to assist and inform me if the code itself is correct/ incorrect and how i should go about amending the code, thanks in advance and have a wonderful end to the year thanks!
Your trigger should be like :
CREATE OR REPLACE TRIGGER trg_voltype
BEFORE INSERT ON volunteer
FOR EACH ROW
BEGIN
if :new.Volunteertype = 'STUDENT'
then
:new.Area_of_work := NULL;
:new.StaffManagerName := NULL;
:new.StaffManagerEmail := NULL;
:new.StaffManagerPhone := NULL ;
end if;
END;

Trigger set insert row values to null [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I'm trying to write a trigger that when something is inserted into the database if it is over a specific time set the row values to null
CREATE OR REPLACE TRIGGER hi
AFTER INSERT OR UPDATE OF CLASS_TIME ON class
FOR EACH ROW
BEGIN
IF (:NEW.CLASS_TIME < '09:00' )
OR (:NEW.CLASS_TIME > '18:00' )
THEN
RAISE_APPLICATION_ERROR(-20000, 'Due to low attendance no class cannot be scheduled at that time');
SET NEW.STAFFNO = NULL;
SET NEW.CLASS_DAY = NULL;
SET NEW.CLASS_TYPE = NULL;
SET NEW.ROOMNUM = NULL;
END IF;
END;
All that I have found online shows that what Ive got is correct but I get the error on the STAFFNO. Thanks in advance for any input.
First of all you have to decide:
either you want the insert or update to fail when it's being done at undesired time
or you want the insert or update to be processed, but with the columns set to null then.
You cannot have both.
In case you want to set the columns to NULL, this should be a BEFORE INSERT/UPDATE trigger, so the changed columns get written to the table. (In an AFTER INSERT/UPDATE trigger setting the fields to some value would not have any effect, becase they are not written.)
Then SET NEW.STAFFNO = NULL; is no valid PL/SQL, that would have to be :NEW.STAFFNO := NULL; instead.
CREATE OR REPLACE TRIGGER hi
BEFORE INSERT OR UPDATE OF class_time ON class
FOR EACH ROW
WHEN (new.class_time NOT BETWEEN '09:00' AND '18:00')
BEGIN
:new.staffno := null;
:new.class_day := null;
:new.class_type := null;
:new.roomnum := null;
END;

how to use triggers when comparing two data from different tables? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
CONSTRAINT (Movie.Date – person.DateOfBirth >= Movie.MinAge)
I am trying to use triggers for this constraint, I am not sure how to use triggers when you are comparing different data from different tables.
Something like this.. I didn't test it!!
CREATE OR REPLACE TRIGGER check_movie_parent_rating
BEFORE INSERT OR UPDATE OF DateOfBirth ON Person
REFERENCING NEW AS n
FOR EACH ROW
DECLARE
v_allowed VARCHAR2(1);
BEGIN
BEGIN
SELECT CASE WHEN (Movie.Date - person.DateOfBirth >= Movie.MinAge)
THEN 'Y'
ELSE 'N'
END As Allowed
INTO v_allowed
FROM Movie , Person
WHERE Movie.id = :n.movieId
AND Person.movieId = Movie.id
AND Person.id = :n.id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_allowed := NULL;
END;
IF v_allowed IS NULL THEN
Raise_application_error(-20202, 'Movie Detail not available in parent');
ELSIF v_allowed = 'N' THEN
Raise_application_error(-20201, 'Movie Restricted for User');
END IF;
END;
/
Here is a solution that will avoid the mutating table error that arises when you select from a table within a row level trigger defined against the same table.
You should check the constraint whenever any involved table is changed.
Ideally, there should also be serialisation locks at within the trigger to ensure transactions executing concurrently do not alter the data in such a way as to break the constraint. This can be achieved by using modules within the supplied DBMS_LOCK package.
CREATE OR REPLACE TRIGGER Movie_Check_Age
AFTER INSERT OR UPDATE OF <Join_Columns>, Date, MinAge ON Movie
FOR EACH ROW
DECLARE
CURSOR csrPersons
IS
SELECT NULL
FROM Person p
WHERE p.<Join_Columns> = :new.<Join_Columns>
AND (Months_Between(:new.Date, p.DateOfBirth) / 12) < :new.MinAge;
rPerson csrPersons%ROWTYPE;
BEGIN
OPEN csrPersons;
FETCH csrPersons INTO rPerson;
IF csrPersons%FOUND THEN
CLOSE csrPersons;
Raise_Application_Error(-20001, 'Person too young to see movie');
ELSE
CLOSE csrPersons;
END IF;
END;
/
CREATE OR REPLACE TRIGGER Person_Check_Age
AFTER INSERT OR UPDATE OF <Join_Columns>, DateOfBirth ON Person
FOR EACH ROW
DECLARE
CURSOR csrMovies
IS
SELECT NULL
FROM Movie m
WHERE m.<Join_Columns> = :new.<Join_Columns>
WHERE (Months_Between(m.Date, :new.DateOfBirth) / 12) < m.MinAge;
rMovie csrMovies%ROWTYPE;
BEGIN
OPEN csrMovies;
FETCH csrMovies INTO rMovie;
IF csrMovies%FOUND THEN
CLOSE csrMovies;
Raise_Application_Error(-20001, 'Person too young to see movie');
ELSE
CLOSE csrMovies;
END IF;
END;
/

Oracle - Is there any way I can get around SELECT INTO on PL/SQL block

I am trying to create a trigger, which automatically updates a student's application state when the application status row in the application table changes. I have been browsing the web for a little over an hour or so now and despite finding a potential work around using EXECUTE IMMEDIATE I cannot achieve my desired result (EXECUTE IMMEDIATE was causing an unbound variable error).
Trigger code
CREATE OR REPLACE TRIGGER trg_applications
BEFORE INSERT OR UPDATE ON applications FOR EACH ROW
BEGIN
IF UPDATING THEN
/* If the status is ACCEPTED, then approve the students application */
SELECT CASE
WHEN get_status(:NEW.status_id) =
LOWER('Applicant Accepted Offer')
THEN student_accept_offer( :NEW.student_id )
END
FROM status;
END IF;
END;
The get status method returns a VARCHAR2 to check whether the new status matches the condition, if so I want to update the student_approved row using the autonomous_transaction below.
student_accept_offer code
CREATE OR REPLACE FUNCTION student_accept_offer( this_stu_id NUMBER )
RETURN VARCHAR2 IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
UPDATE students
SET students.student_on_placement = 1
WHERE students.student_id = this_stu_id;
COMMIT;
RETURN 'Student has approved application';
END student_accept_offer;
This function works as intended when I test it outside of my trigger, however when it is embedded in the trigger an PLS-00428 error gets thrown. Could anyone point me in the right direction as to how can I work around this, to allow me to have this function fire automatically on an update if the status matches.
Thanks for your time
EDIT - Tables I am referencing
Changing your code slightly to remove the SELECT statement (as it seems unnecessary) then does this work?
CREATE OR REPLACE TRIGGER trg_applications
BEFORE INSERT OR UPDATE ON applications FOR EACH ROW
BEGIN
IF UPDATING THEN
/* If the status is ACCEPTED, then approve the students application */
IF get_status(:NEW.status_id) = 'applicant accepted offer' THEN
student_accept_offer( :NEW.student_id );
END IF;
END IF;
END;