I'm getting an error while excuting a trigger statement - exception

enter image description here CREATE OR REPLACE TRIGGER weekd_tri
BEFORE INSERT OR UPDATE ON payment
FOR EACH ROW
BEGIN
IF TO_CHAR(SYSDATE, 'D') = '1' THEN
RAISE_APPLICATION_ERROR(-20000, 'Cannot make a payment on weekends');
END IF;
END;
error msg
ORA-06512: at "SCOTT.WEEKD_TRI", line 3
ORA-04088: error during execution of trigger 'SCOTT,WEEKD_TRI
can you tell me why i'm getting this error and how to solve this error
*
*this trigger should not accept payment by customer when made on Sat/Sun so i wrote this trigger but when I stried to insert data on Sun it did Fire a triger ORA - 20000 Cannot make a payment on weekends and I was not able to update the values but apart from that I got the other two Erros ORA-06512 & ORA:04088 **

As from your screenshot I see there are 3 error message lines
ORA-20000 - which is the real error message that has been caused from your logic
ORA-6512 - service message to identify the line of code error occured
ORA-4088 - another service message to identify trigger name where error occured
So, the reason why you're getting ora-4088 is raise_application_error command you're using for exception firing. It is just a part of service message for developers

There's no "error", except the one you created yourself, intentionally.
Which day is it today?
SQL> select to_char(sysdate, 'D') from dual;
T
-
1 --> OK, that's the value you're checking in a trigger
SQL>
Sample table:
SQL> create table payment (id number);
Table created.
Trigger:
SQL> create or replace trigger weekd_tri
2 before insert or update on payment
3 for each row
4 begin
5 if to_char(sysdate, 'D') = '1' then
6 raise_application_error(-20000, 'Cannot make a payment on weekends');
7 end if;
8 end;
9 /
Trigger created.
Testing:
SQL> insert into payment (id) values (1);
insert into payment (id) values (1)
*
ERROR at line 1:
ORA-20000: Cannot make a payment on weekends
ORA-06512: at "SCOTT.WEEKD_TRI", line 3
ORA-04088: error during execution of trigger 'SCOTT.WEEKD_TRI'
Right; that's EXACTLY what you wanted to do: IF concluded that today really is '1' and raised an error.
If it were e.g. '2' you checked, today you'd be able to insert a row:
SQL> create or replace trigger weekd_tri
2 before insert or update on payment
3 for each row
4 begin
5 if to_char(sysdate, 'D') = '2' then
6 raise_application_error(-20000, 'Cannot make a payment on weekends');
7 end if;
8 end;
9 /
Trigger created.
SQL> insert into payment (id) values (2);
1 row created.
SQL>

Related

mysql: I didn't update the table defining the trigger in the trigger, but i got the similar error

I have three tables. They are called book, reader, borrow.
The function of my trigger: insert borrowing records into the borrow table. But before inserting, we have to check the status of the reader's library card and the status of the book. Only when they are all normal can we lend the book to the reader, that is, change the status of the borrowed book in the book table to "borrowed", and then insert the borrowing record into the borrow table. However, some errors occurred when updating the book table.
Please look at my note 1, which reported me such an error: [stored trigger is already used by statement which invoked stored trigger]. I know that the trigger of MySQL can't modify the defined table, so I think this error is very puzzling. Then I created a new account table and added the sentence marked in Note 2 to the trigger. It works properly. So this mistake really confused me.
I would be very grateful if you could help me. Followings is my code:
DROP TRIGGER IF EXISTS before_insert_borrow;
CREATE TRIGGER before_insert_borrow BEFORE INSERT ON borrow
FOR EACH ROW
BEGIN
DECLARE cst VARCHAR(10);
DECLARE b_cts VARCHAR(10);
SELECT Card_status FROM reader WHERE read_no = NEW.read_no INTO cst;
SELECT B_status FROM book WHERE book_no = NEW.book_no INTO b_cts;
IF cst = 'loss' THEN
SIGNAL SQLSTATE '09000' SET MESSAGE_TEXT = 'your card is lost……';
ELSEIF b_cts = 'checked' THEN
SIGNAL SQLSTATE '09000' SET MESSAGE_TEXT = 'the book you want has been checked out.';
END IF;
UPDATE book SET B_status = 'checked' WHERE book_no = NEW.book_no; -- 1
UPDATE account SET total = total + 1000 WHERE id =1; -- 2
END;
-- test data
INSERT INTO borrow(read_no,book_no)VALUES('001','B17682');

How to decrement a value in another table via a trigger? (MySQL)

I want my trigger to decrement the quantity by 1 in the table "quantity" when a new row is added to "rented_equipment_log" where date_returned has a value of NULL.
A basic synopsis of the database is that there is an equipment table with columns; model, maker, type and quantity. (E.g. 1001, 'Jackson', 'oar', 10)
The table rented_equipment_log has the columns; member_id, model, type, date_taken, date_returned. (E.g. 17225663, 1001, oar, 2018-11-26, 2018-11-27)
So when a member takes out a piece of equipment but has not yet returned it (date_returned is null), the quantity of the same model decrements by 1 in the table equipment.
However I'm getting a syntax error. I've looked at other questions similar to this and still can't figure out what the error is.
Here's the trigger:
delimiter //
CREATE TRIGGER UpdateQuantity
AFTER INSERT ON rented_equipment_log
FOR EACH ROW BEGIN
DECLARE q integer;
SELECT quantity INTO q FROM equipment WHERE model = NEW.model;
IF (date_returned IS NULL) THEN
UPDATE equipment
SET quantity = q -1 WHERE model = NEW.model
END IF;
END;//
And here's the error:
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near
'END IF;
END' at line 9
I'm not sure why you are getting an error there. But your query is more complicated than necessary:
delimiter //
CREATE TRIGGER UpdateQuantity
AFTER INSERT ON rented_equipment_log
FOR EACH ROW
BEGIN
UPDATE equipment e
SET e.quantity = e.quantity - 1
WHERE e.model = NEW.model and ?.date_returned IS NULL;
END;//
I'm not sure if date_returned should be new.date_returned or e.date_returned. I don't even understand that condition. I would be expecting e.equipment > 0.

'RAISERROR' must be declared

CREATE TRIGGER x AFTER INSERT ON itemtype
FOR EACH ROW
DECLARE
minn itemtype.PRICE%type;
BEGIN
select MIN(itemtype.PRICE) into minn from itemtype;
IF (:new.PRICE > minn*4) then RAISERROR('Custom text');
END IF;
END;
/
I'm trying to create a trigger that raises an error when I try to insert a new entry into the itemtype with itemtype.PRICE column value is greater than 4 times the current low priced item on the table.
I get these compilation errors when I try to create the trigger.
LINE/COL ERROR
-------- --------------------------------------------------------------
5/31 PL/SQL: Statement ignored
5/31 PLS-00201: identifier 'RAISERROR' must be declared
I have also tried
CREATE TRIGGER x AFTER INSERT ON itemtype
FOR EACH ROW
DECLARE
minn itemtype.PRICE%type;
BEGIN
select MIN(itemtype.PRICE) into minn from itemtype;
if (:new.PRICE > minn*4) then raise_application_error(-20010,'Too Expensive');
END IF;
END;
/
which complies, but when i try to insert a new entry into the table I get theses errors saying my trigger fails.
SQL> insert into itemtype(ITEMNUM,NAME,PICTURE,PRICE,BELONGSTO ) VALUES ('A11','The who knows','',10.99,'P');
insert into itemtype(ITEMNUM,NAME,PICTURE,PRICE,BELONGSTO ) VALUES ('A11','The who knows','',10.99,'P')
*
ERROR at line 1:
ORA-04091: table USERNAME.ITEMTYPE is mutating, trigger/function may not see it
ORA-06512: at "USERNAME.X", line 5
ORA-04088: error during execution of trigger 'USERNAME.X'
Try use PRAGMA AUTONOMOUS_TRANSACTION in trigger.
Look my answer on this question:
SQL trigger on delete mutating table

Create trigger that gives warning/error

Need help for creating trigger using phpmyadmin that will give warning before inserting data. The situation is much like this:
CREATE OR REPLACE TRIGGER secure_item
BEFORE INSERT
ON item
BEGIN
IF (TRIM(TO_CHAR (SYSDATE, 'DAY')) IN ('SATURDAY', 'SUNDAY'))
OR ( TO_NUMBER(TO_CHAR(SYSDATE, 'HH24')) NOT BETWEEN 8 AND 15 )
THEN
raise_application_error (-20500,
'You may insert into test table only during business hours.'
);
END IF;
END;
/
Replace sysdate with
NOT hour(now()) between 8 and 15 OR weekday(now()) > 4

Mysql stored procedure SQLEXCEPTION

Below is my sample query, in which 1st create new folder,2nd update count, 3rd: create new tag, 4: get new id.all query are executed in same stored procedure.in which #pFolderId is the id passed by user(in parameter),
foreign key constraint are applied on folderId of tag table, when user sends valid folderId, it executes properly & i get new tagid, but when user sends invalid folderid which is not exists in folder table, then it throws exception of foreign key. but in this case 1st,2nd query is alredy executed, 3rd query gives error. my question is how to avoid query from execution on any error on any query. 1 way is to use roolback, but when i used that it execute properly for 1 invalid folder request, but when on 2nd request it executed 1,2 query and gave error on 3rd.
1: insert into folder (name)values("new");
2: update folder set count = count + 1 where idfolder=1;
3: insert into tag(name,folder_Id)values("dsdss",#pFolderId );
4: SELECT last_insert_id() AS tag_id;
how can i achieve this, avoid execution on error.any help will be appreciated. thank you in advance.
PROCEDURE:
DELIMITER $$
CREATE DEFINER=root#localhost PROCEDURE newClip(pFolderId VARCHAR(100))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION,SQLWARNING
BEGIN
SELECT SQLEXCEPTION.message;
ROLLBACK;
END;
SET #pFolderId = pFolderId;
START TRANSACTION;
insert into folder (name)values("new");
update folder set count = count + 1 where idfolder=1;
insert into tags(name,folderId)values("dsdss",#pFolderId );
SELECT last_insert_id()AS tag_id;
COMMIT;
END$$