I have created a trigger for my mysql project previously and it was working well. However, I am trying to change this trigger to make suitable for pl-sql(oracle). This is my original code which works in the mysql:
DELIMITER $$
CREATE TRIGGER course_title_delete AFTER DELETE on Course
FOR EACH ROW
BEGIN
DECLARE rownumber INT;
SET rownumber = (SELECT COUNT(*) FROM Course
WHERE Course_code=old.Course_code);
IF rownumber = 0
THEN
DELETE FROM Course_title
WHERE Course_title.Course_code=old.Course_code;
END IF;
END$$
DELIMITER ;
And this one is the same code which I have tried to convert pl-sql format. However it is not working, when I upload as a script and try to run it in apex.
CREATE OR REPLACE TRIGGER course_title_delete
AFTER DELETE ON course
FOR EACH ROW
BEGIN
DECLARE rownumber INT;
SET rownumber = (SELECT COUNT(*) FROM course
WHERE course_code= :old.course_code);
IF rownumber = 0
THEN
DELETE FROM course_title
WHERE course_title.course_code:=:old.course_code;
END IF;
END;
/
At the very least, you have a syntax error in this line:
DELETE FROM course_title
WHERE course_title.course_code:=:old.course_code;
----------------------------------^
That should just be =.
Also, in Oracle, the DECLARE goes before the BEGIN, not after.
Also, in Oracle, this line:
SET rownumber = (SELECT COUNT(*) FROM course
WHERE course_code= :old.course_code);
should be:
SELECT COUNT(*) INTO rownumber
FROM course
WHERE course_code = :old.course_code;
Well, actually, the is the correct syntax for what you are expressing. But, you should be using NOT EXISTS rather than COUNT(*) in both databases.
In both databases, I think you can replace this trigger with a cascading delete foreign key constraint. Also, you can simplify the logic to eliminate the if:
CREATE OR REPLACE TRIGGER course_title_delete
AFTER DELETE ON course
FOR EACH ROW
BEGIN
DELETE FROM course_title
WHERE NOT ExISTS (SELECT 1
FROM course c
WHERE c.course_code = :old.course_code
) AND
course_code = :old.course_code;
END;
Related
Please support, I had a stored procedure create in sql server database using a Merge statement inside. I would like to use the same stored procedure in a Mysql database. Unfortunatly it seem the Merge funtion not work in MySql. Anybody can help me to do that ? Below my stored procedure
ALTER PROCEDURE [dbo].[ValiderFacturePharmacie]
#numdossierhospi VARCHAR(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
MERGE INTO pharmacie P
USING (SELECT NumDossHp, SUM(TotalProd) AS Total
FROM pharmacie WHERE NumDossHp = #numdossierhospi
GROUP BY NumDossHp) T
ON (P.NumDossHp = T.NumDossHp)
WHEN MATCHED THEN
UPDATE SET
P.TotalPharma = T.Total,
P.Etat ='VALIDE';
END
Thanks you for your answer ! I solved the issue by using this statement below
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ValiderFacturePharmacie`(IN `numdossierhospi` VARCHAR(30))
NO SQL
UPDATE pharmacies
SET totalpharma = (select SUM(totalprod) from pharmacies where numdosshp = numdossierhospi),
etat = 'FACTURER'
WHERE numdosshp = numdossierhospi$$
DELIMITER ;
It is alomst the same
DELIMITER //
CREATE PROCEDURE ValiderFacturePharmacie (
_numdossierhospi VARCHAR(50))
BEGIN
UPDATE pharmacie P INNER JOIN
(SELECT NumDossHp, SUM(TotalProd) AS Total
FROM pharmacie WHERE NumDossHp = _numdossierhospi
GROUP BY NumDossHp) T
ON (P.NumDossHp = T.NumDossHp)
SET
P.TotalPharma = T.Total,
P.Etat ='VALIDE';
END //
DELIMITER ;
I want to create a Trigger in MySql I did it with SQL Server at school in my class, but for my project, my web hoster only give me MySql DataBase and if I want a SQL Server DB I need to buy a VPS and I can't afford it so i'll do with MySql, I read about MySql Trigger and I saw that they are really more hard to do than SQL Server Trigger.
First here is every article I read about trigger in MySql (To be sure to not be placed as a duplica):
https://www.mysqltutorial.org/mysql-triggers.aspx/
https://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx/
https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
https://stackoverflow.com/questions/38745441/how-to-write-trigger-with-multiple-condition-in-mysql
https://stackoverflow.com/questions/33775826/how-to-declare-variables-in-trigger-with-mysql
And here is the SQL Server code I made:
CREATE TRIGGER file_verified_if_admin ON tblOpenSource
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE #source_id INT;
DECLARE #verified TEXT;
DECLARE #client_id INT;
DECLARE c_info CURSOR FOR
SELECT source_id, verified, client_id FROM inserted;
OPEN c_info;
FETCH c_info INTO #source_id,#verified,#client_id;
WHILE (##FETCH_STATUS=0)
BEGIN
IF ((SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblOpenSource.client_id = tblClients.client_id;) == 'admin')
BEGIN
UPDATE tblOpenSource SET verified = 'verified' WHERE source_id = #source_id;
END;
END;
CLOSE c_info;
DEALLOCATE c_info;
END;
Here is what I made with MySql and I still have errors, can someone help me ?
CREATE TRIGGER file_verified_if_admin
AFTER INSERT, UPDATE ON tblOpenSource
FOR EACH ROW
BEGIN
DECLARE client_grade text;
SET #client_grade = (SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblClients.client_id = tblOpenSource.client_id);
IF (#client_grade = 'admin')
UPDATE tblOpenSource SET verified = 'verified' WHERE source_id = NEW.source_id;
END;
It says I have an error near my DELCARE, but I don't understand why, because I heard we don't need an "#" in the declaration.
What it is supposed to do, is to look if the file as been publied by an admin and if yes, it should update the verified column and change the text for verified
Mysql desn_'t allow multiple trigger in one goal you have to define then all.
Maysql needs DELIMITERto know where the trigger starts and ends
Last the IÌF CLause`is in mysql different.
This is the correct syntax, you can change it further by using SELECT INTO
UPDATE #Akina poited out,
that your query will not run, you can't change a row that has triggert the Trigger, mysql will not not allow that, so for your insert trigger, has to be change accordingly.
For the UPDATE Trigger goes teh saem.
DELIMITER //
CREATE TRIGGER file_verified_if_admin
BEFORE INSERT ON tblOpenSource
FOR EACH ROW
BEGIN
SET #client_grade = (SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblClients.client_id = tblOpenSource.client_id);
IF (#client_grade = 'admin') then
SET NEW.verified = 'verified';
END IF;
END//
DELIMITER ;
I Have a Database in mysql, i have a principal table that use a column name cedula VARCHAR (50), the column have characters, but i want to change it with a consecutive number. I tried with this:
SET #a:=1;
UPDATE bdpiiad2.tpersona
SET cedula=(CONVERT(#a:=#a+1, CHAR(50)))
But i have 2 problems, first doesn't change, and second I'm not sure if change the other ids in the others tables that have a reference.
try using this
SET #a:=1;
UPDATE bdpiiad2.tpersona
SET cedula=(CAST(#a:=#a+1 as CHAR));
to update all you need to create a procedure like this
DELIMITER $$
USE `bdpiiad2`$$
DROP PROCEDURE IF EXISTS bdpiiad2.`up`$$
CREATE PROCEDURE `up`()
BEGIN
DECLARE max_cnt INT;
DECLARE cnt INT;
SET cnt=1;
UPDATE bdpiiad2.tpersona SET cedula='0';
SELECT COUNT(*) INTO max_cnt FROM bdpiiad2.tpersona;
WHILE cnt<=max_cnt DO
UPDATE bdpiiad2.tpersona
SET cedula=(CAST(cnt AS CHAR))
WHERE cedula='0' LIMIT 1;
SET cnt=cnt+1;
END WHILE;
END$$
DELIMITER ;
and then call it like this
CALL bdpiiad2.up();
I trying execute an trigger on mysql database.
The command executes successfully, but the trigger not working.
DELIMITER #
CREATE TRIGGER generate_coupon AFTER INSERT ON order
FOR EACH ROW
BEGIN
DECLARE userid, couponvalue INT;
DECLARE couponcode VARCHAR;
SELECT idUser INTO userid FROM indication WHERE email = NEW.email;
SET couponvalue = 20;
SET couponcode = 'abc123';
INSERT INTO coupon(idUser,idOrder,couponvalue,couponcode) values(userid, NEW.id, couponvalue, couponcode);
END#
DELIMITER ;
I suspect your problem arises from the collisions between your variables couponvalue and couponcode with the same-named columns in your coupon table. As documented under Local Variable Scope and Resolution:
A local variable should not have the same name as a table column.
You could simplify your trigger to the following and, in so doing, avoid this problem entirely:
CREATE TRIGGER generate_coupon AFTER INSERT ON order FOR EACH ROW
INSERT INTO coupon
(idUser, idOrder, couponvalue, couponcode)
SELECT idUser, NEW.id, 20, 'abc123'
FROM indication
WHERE email = NEW.email
;
Basically i'm going to run this procedure anytime a student enrolls/drops a course.
I'm trying to set student_total = # of students in a course, then update that corresponding section with (student_total + 1) i'm having trouble finding good documentation for stored procedures. I'm getting an error on my Declare student_total int; line. What am i not doing correct?
DELIMITER $$
CREATE PROCEDURE `mydb`.`update_seats` (IN section_ID varchar(20))
BEGIN
SET #section_id=section_id;
DECLARE student_total int;
-- count the number of students in the course --
SET student_total = SELECT count(student_ID) from course
WHERE section_ID = #section_id;
Update Section SET students_enrolled = (student_total + 1)
WHERE section_ID = #section_id;
END
Problems
Error 1
From MySql documentation:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
So, you should move DECLARE... statement before the SET #section_id... statement.
Error 2
You are trying to select a value into a variable using invalid snytax! You should use SELECT ... INTO instead of SET ... = SELECT ... (which is invalid syntax).
Removing Redundancy
No need to assign parameter (section_ID) to a global variable (#section_ID). You can simply change the parameter name to avoid name collision with section.section_ID column.
Solution
DELIMITER ;;
CREATE PROCEDURE `update_seats` (IN p_section_ID VARCHAR(20))
BEGIN
DECLARE student_total INT;
SELECT count(student_ID)
INTO student_total
FROM course
WHERE section_ID = p_section_ID;
UPDATE section
SET students_enrolled = (student_total + 1)
WHERE section_ID = p_section_ID;
END;;
DELIMITER ;
You're using the command line tool, yes?
It would be helpful to know the error message you received but based off the code you posted I don't see where you reset the delimiter command after the BEGIN...END block.
Taken from http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html