Create a trigger that checks dates [problem] - mysql

I have a problem .. What is required is the creation of a trigger to check if the date of the destruction(dateEmpr) is less than the date of return(dateRetEff), it is increased 10 days to the date of the return
table structure
CREATE TABLE emprunter(
numLivre VARCHAR(5),
dateEmpr DATE,
numInsc VARCHAR(5),
dateRetEff DATE
);
This is my code .. and it tells me it's wrong
CREATE TRIGGER verifier_date
BEFORE INSERT ON emprunter
FOR EACH ROW
BEGIN
if((SELECT DATEDIFF(NEW.dateRetEff, NEW.dateEmpr) from emprunter ) <0) then
dateEmpr = DATE_ADD(OLD.dateEmpr, INTERVAL 10 DAY);
end if;
END;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= DATE_ADD(OLD.dateEmpr, INTERVAL 10 DAY)' at line 6

CREATE TRIGGER verifier_date
BEFORE INSERT ON emprunter
FOR EACH ROW
BEGIN
if NEW.dateRetEff < NEW.dateEmpr then
SET NEW.dateEmpr = NEW.dateEmpr + INTERVAL 10 DAY;
end if;
END;
fiddle

Related

DATEADD() function doesn't return the adding value

I wanted to create a funcition that can add 30 days to the input date, but I got an error message: #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 '30, last_date);
END'
Here is my code:
DELIMITER //
CREATE FUNCTION expected_date (
last__date DATE
)
RETURNS DATE
BEGIN
RETURN DATE_ADD(DAY, 30, `last_date`);
END; //
DELIMITER ;
How should I fix it?
CREATE FUNCTION expected_date (
pdate DATE
)
RETURNS DATE
BEGIN
RETURN DATE_ADD(pdate, interval 30 day);
END;

mySql query error when I try to use and IF statement

I'm not able to find the error in MySQL query.
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 '' at line 10
When I remove:
IF DAYOFWEEK(curdate()) BETWEEN 2 AND 6 THEN
END IF;
Then the query executes fine.
I've tried using tab/space inside my IF statement
CREATE EVENT UPLOADTASK
ON SCHEDULE EVERY '1' DAY
STARTS '2019-07-09 00:00:00'
DO
IF DAYOFWEEK(curdate()) BETWEEN 2 AND 6 THEN
INSERT INTO TABLE1 VALUES
(1,"TASK1",curdate()-1,NULL),
(6.5,"TASK2",curdate()-1,NULL),
(3.0,"TASK3",curdate()-1,NULL),
(8,"TASK44",curdate()-1,NULL);
END IF;
Include your If inside Begin End block
CREATE EVENT UPLOADTASK
ON SCHEDULE EVERY '1' DAY
STARTS '2019-07-09 00:00:00'
DO
BEGIN
IF DAYOFWEEK(curdate()) BETWEEN 2 AND 6 THEN
INSERT INTO TABLE1 VALUES
(1,"TASK1",curdate()-1,NULL),
(6.5,"TASK2",curdate()-1,NULL),
(3.0,"TASK3",curdate()-1,NULL),
(8,"TASK44",curdate()-1,NULL);
END IF;
END

scheduler events in mysql using if conditions

I have written the below query
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
DECLARE cnt1 AS BIGINT
SELECT COUNT(*) AS cnt1, d.Invoice_Date AS Invoice_Date1 FROM Depot_Sales__c AS d, Advance_Payment_Request__c A, Supplier_Payment__c S WHERE d.Supplier_Code=A.Supplier AND d.Supplier_Code=S.Supplier AND S.Supplier=80
IF cnt1=0 THEN
SELECT COUNT(*) FROM Depot_Sales__c AS d
END IF;
I am getting the below error
Error Code : 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 'declare cnt1 as bigint
SELECT COUNT(*) as cnt1, d.Invoice_Date as Invoice_Date1 ' at line 8
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 'IF cnt1=0 THEN' at line 10
Why am I getting this error?
The problem is that you can only use the DECLARE statement inside a BEGIN..END block, and only at the beginning of that block. See the MySQL documentation at http://dev.mysql.com/doc/refman/5.0/en/declare.html.
You'll need to wrap your do statement in a BEGIN...END block. And I think you'll also need to change the delimiter so you can do more than one statement.
So, it would end up being something like:
delimiter |
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
BEGIN
DECLARE cnt1 AS BIGINT;
SELECT COUNT(*) AS cnt1, d.Invoice_Date AS Invoice_Date1 FROM Depot_Sales__c AS d, Advance_Payment_Request__c A, Supplier_Payment__c S WHERE d.Supplier_Code=A.Supplier AND d.Supplier_Code=S.Supplier AND S.Supplier=80;
IF cnt1=0 THEN
SELECT COUNT(*) FROM Depot_Sales__c AS d;
END IF;
END |
delimiter ;

MySql CREATE EVENT 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$$ DELIMITER' at line 4
CREATE EVENT event_1 ON SCHEDULE EVERY 1 WEEK STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY DO BEGIN
UPDATE
sonice_User
SET
update_chance = "1"; END
use below code:
Delimiter $$
CREATE EVENT event_1 ON SCHEDULE EVERY 1 WEEK
STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY
DO
BEGIN
UPDATE sonice_User
SET update_chance = "1";
END $$
Delimiter ;
CREATE EVENT event_1 ON SCHEDULE EVERY 1 WEEK
STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY
DO
UPDATE sonice_User
SET update_chance = "1";

SQL Trigger solve

CREATE TRIGGER Print
Before UPDATE ON employ
FOR EACH ROW
WHEN (NEW.Employe_ID>0)
DECLARE
salary int;
BEGIN
salary:= :NEW.salary-:OLD.salary;
dbms_output.put('Old salary:'||:OLD.salary);
dbms_output.put('New salary:'||:NEW.salary);
dbms_output.put_line('Difference'||salary);
END;
/
Shows
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 'WHEN (NEW.Employe_ID>0)
DECLARE
salary int' at line 4
I have a table name employe which has 4 columns Employe_ID, E_name,
Department_ID,
salary
What is the problem with this sql and how can I solve it?
I don't get why you added the condition on employee_id > 0, but in case you actually need it
delimiter //
CREATE OR REPLACE TRIGGER print BEFORE UPDATE ON employee
FOR EACH ROW
IF ( NEW.employe_id > 0 ) THEN
SELECT NEW.salary - OLD.salary INTO #delta;
END IF;
//
delimiter ;
Insert and update your data, then read #delta
insert into employee select 1,'Me',1,2400;
update employee set salary = 2800 where id = 1;
Select #delta;
I don't think it's possible to automatically output #delta to the screen as it would be on Oracle using dbms_output.put_line().