oracle trigger count one table after insert update another - mysql

I have this trigger in mysql which works fine:
CREATE TRIGGER ins_trainee AFTER INSERT ON trainee
FOR EACH ROW UPDATE training_activities SET NO_of_Trainees =
( SELECT COUNT(SN)
FROM trainee
WHERE trainee.training_type = training_activities.SN
)
And I tried converting to oracle like this:
CREATE OR REPLACE TRIGGER ins_trainee
AFTER INSERT ON trainee
FOR EACH ROW
BEGIN
UPDATE training_activities SET NO_of_Trainees =
SELECT COUNT(SN)
FROM trainee
WHERE trainee.training_type = training_activities.SN;
END;
/
But I get warning: trigger created with compilation errors
And show errors gives:
2/1 PL/SQL: SQL Statemnet Ignored
2/49 PL/SQL: ORA-00936: missing expression

Related

'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

Syntax error when add trigger

I have procedure:
CREATE PROCEDURE channge_max_priority()
BEGIN
update feeds set max_post_priority =
(
select max(feed_posts.priority)
from feed_posts
where feed_posts.feed_id = feed_id
)
where feeds.id = feed_id;
END
And I want to create three triggers:
CREATE TRIGGER feed_post_after_test AFTER INSERT (And update and delete ) ON feed_posts
FOR EACH ROW EXECUTE PROCEDURE change_max_priority();
Procedure work correctly, but when I try start trigger - I get error:
Error in query (1064): Syntax error near 'PROCEDURE change_max_priority()' at line 2
You have a double nn in name procedure :
replace :
CREATE PROCEDURE channge_max_priority()
by
CREATE PROCEDURE change_max_priority()
Try changing the update statement to use a join instead, this should work:
update feeds
set max_post_priority = max(feed_posts.priority)
FROM feeds f
INNER JOIN feed_posts fp on feed_posts.feed_id = f.feed_id
Group BY f.feed_id
In mysql, a procedure is executed using CALL:
CREATE TRIGGER feed_post_after_test AFTER INSERT (And update and delete ) ON feed_posts
FOR EACH ROW CALL change_max_priority();
I don't see why you should execute the procedure for each row though, the whole feeds table is update in one procedure call.
Also not sure the last where feeds.id = feed_id; (just before END) is correct in the procedure.

Mysql said: #1422 - Explicit or implicit commit is not allowed in stored function or trigger

In the code below I'm trying to create a trigger which inserts 'incidentimg' table id along with an image path if the user of the web application did not upload a image with the form belonging to 'incidentreport' table, however, I'm only getting the error "MySQL said: #1422 - Explicit or implicit commit is not allowed in stored function or trigger"
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
IF(SELECT incidentimg.incidentImgId FROM incidentimg
LEFT JOIN incidentreport ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL)
THEN INSERT INTO incidentimg(incidentImgId, imgUrl)values(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
;
Here are the tables being used
Can someone assist me on how to correct this problem
I finally got the code to work. Now the code will check whether 'incidentimg' table has all the 'incidentReportId' that the 'incidentreport' table has, and if it doesn't then the last inserted id from the 'incidentreport' table along with a default image path will be inserted into 'incidentimg' table AFTER INSERT.
CREATE TRIGGER insertImage AFTER INSERT ON incidentreport
FOR EACH ROW
BEGIN
IF(SELECT incidentreport.incidentReportId
FROM incidentreport
LEFT JOIN incidentimg ON incidentreport.incidentReportId = incidentimg.imgs_incidentReportId
WHERE incidentimg.imgs_incidentReportId IS NULL)
THEN INSERT INTO incidentimg(imgs_incidentReportId, imgUrl) VALUES(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
I am not much sure but I am guessing that probably it's because of the IF .. THEN conditional INSERT. Try modifying your INSERT statement with an EXISTS like
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
INSERT INTO incidentimg(incidentImgId, imgUrl)
SELECT NEW.incidentReportId, 'images/no-image.png'
FROM DUAL WHERE EXISTS (
SELECT 1 FROM incidentimg
LEFT JOIN incidentreport
ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL
)
END;

MySQL trigger working on one table but not the other

I have created a mysql trigger to update on insert a value from another table:
DROP TRIGGER IF EXISTS `CourseCode`//
CREATE TRIGGER `CourseCode` BEFORE INSERT ON `race`
FOR EACH ROW BEGIN
SET NEW.Course_Code = (
SELECT
course_code
from
tb_course
where
tb_course.course_name = NEW.course_name
)
END
//
This works perfectly. It returns the 4 character code for the course_name based on the tb_course table.
What I'm trying to do is the exact same thing in another table, I copy and paste the trigger and rename the trigger and the table (field names and types are also identical) it but it won't work:
DROP TRIGGER IF EXISTS `CourseCode2`//
CREATE TRIGGER `CourseCode2` BEFORE INSERT ON `fields`
FOR EACH ROW
BEGIN
SET NEW.Course_Code = (
SELECT
course_code
from
tb_course
where
tb_course.course_name = NEW.course_name
)
END
//
However this results in null values. I've tried replacing New.Course_name with a string (i.e. tb_course.course_name="String") and that updates that static value fine so the trigger appears to be working but either it isn't matching the select statement in this table for or it's not setting the Course_Code field...
Is there any sort of debugging you can suggest to figure this out? It's driving me nuts and I don't know what to do to diagnose the problem.
Cheers
I have no idea what is wrong with the second trigger.
However, the 'debugging' part i can maybe help with.
A search of the 'net' will return various software (paid for) that will allow single step debugging. Rather than pay for stuff, all i want is 'print' statements from inside stored function procedures and triggers.
a quick search lead me to someone who has done all the work already.
Debugging Stored Procedures in MySql
I have just set it up in MySql and tried it and it works fine.
Here is some sample code, based on your trigger and the output i got.
DELIMITER $$
USE `testmysql`$$
DROP TRIGGER /*!50032 IF EXISTS */ `CourseCode`$$
CREATE
/*!50017 DEFINER = 'test'#'localhost' */
TRIGGER `CourseCode` BEFORE INSERT ON `race`
FOR EACH ROW BEGIN
DECLARE v_course_code INT;
DECLARE v_course_title VARCHAR(255);
CALL procLog("line 12: Entering CourseCode");
SELECT
course_code, course_title
INTO
v_course_code, v_course_title
FROM
tb_course
WHERE
tb_course.course_name = NEW.course_name;
SET NEW.course_code = v_course_code;
CALL procLog(CONCAT("line_22: ", v_course_title, " code: ", NEW.course_code));
CALL procLog("line 24: Exiting CourseCode");
END;
$$
DELIMITER ;
Query:
CALL setupProcLog();
INSERT INTO `race` (course_name, race_info)
VALUES ('learn_it_02', 'this is another test');
CALL cleanup('query end');
procLog output:
entrytime connection_id msg
2014-02-23 12:32:21 5 line 12: Entering CourseCode
2014-02-23 12:32:21 5 line_22: Learn It Course 02 code: 2
2014-02-23 12:32:21 5 line 24: Exiting CourseCode
2014-02-23 12:32:21 5 cleanup() query end
Ok, a little clumsy maybe but it will help with debugging 'stored' programs.
I will be using it from now on.

MySql Trigger Syntax to lookup a value in a table and update in the updated row

I'm converting some old legacy Interbase database to MySql and I am having some trouble converting triggers. This is what I came so far, but can't seem to understand what are the errors in the following triggers. Can you help fix the syntax?
DELIMITER ^
In these 2 cases it says into new.FinancialCode is incorrect. What is the correct form to accomplish this? (Lookup a value in a table and change it in the updated row)
Create Trigger triggerFinancialCode BEFORE UPDATE ON FLOW
FOR EACH ROW
begin
Select FinancialCode FROM Accounts where AccountCode = new.AccountCode
into new.FinancialCode ;
end ^
Create Trigger triggerFinancialCodePredicted BEFORE UPDATE ON PREDICTED_FLOW
FOR EACH ROW
begin
Select FinancialCode FROM Accounts where AccountCode = new.AccountCode
into new.FinancialCode ;
end ^
Try this code -
CREATE TRIGGER triggerFinancialCode BEFORE UPDATE ON FLOW
FOR EACH ROW
BEGIN
SET #var = NULL;
SELECT FinancialCode INTO #var FROM Accounts WHERE AccountCode = NEW.AccountCode;
SET NEW.FinancialCode = #var;
END
Do the same for second trigger.