mySql query error when I try to use and IF statement - mysql

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

Related

Create a trigger that checks dates [problem]

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

Getting a syntax error in Mysql create procedure statement which comes from the script I have exported from another mysql server [duplicate]

This question already has answers here:
What is wrong with this stored procedure?
(2 answers)
Closed 3 years ago.
The following code generates an error saying there is a syntax error. I couldn't find any syntax error in this. Beside that, it cant have any syntax error since it has been imported in the form of script from another MySql server unless there is an update in the MySql language itself. if the error is due to the version difference, please can someone throw some light on how the code should be in the newer version.
CREATE DEFINER=`root`#`localhost` PROCEDURE `iterate_day` (IN `p1` TIME, IN `p2` TIME) BEGIN
SET #et = DATE_ADD(NOW(), INTERVAL 1 DAY);
SET #currt = DATE_ADD(NOW(), INTERVAL 1 DAY);
SET #lastday = DATE_ADD(NOW(), INTERVAL 30 DAY);
SET #startHour = p1;
SET #currHour = p1;
SET #endHour = p2;
WHILE #currt<=#lastday DO
WHILE #currHour < #endHour DO
SET #currHour = ADDTIME(#currHour,'00:30:00');
INSERT INTO `blooddb`.`appointments` (`Date`, `timeStart`, `timeEnd`, `status`, `client_id`, `app_id` ) VALUES (#currt, #startHour, #currHour, 1, NULL, NULL) ON DUPLICATE KEY UPDATE `date` = `date` ;
END WHILE;
SET #currt = DATE_ADD(#currt, INTERVAL 1 DAY);
SET #startHour = p1;
SET #currHour = p1;
END WHILE;
END
The error:
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 '' at line 2
Can someone indicate whats wrong in the code
To avoid MySQL thinking the ; within the procedures are ending the create procedure, you need to change the delimter for the end of a statement. Currently it is ;. You can set it to literally anything else.
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
The MySQL documentation suggests //.
Don't forget to set it back to ;.
delimiter //
CREATE DEFINER=`root`#`localhost` PROCEDURE `iterate_day` (IN `p1` TIME, IN `p2` TIME) BEGIN
SET #et = DATE_ADD(NOW(), INTERVAL 1 DAY);
...
END
//
delimter ;

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";

phpMyAdmin can't find syntax for Create EVENT

-mysql 5.6.2
-GLOBAL event_scheduler = ON
Using phpMyAdmin client on MYSQL database. I'm not setting a Delimiter, as I know you can't in this statement. If I remove the last ';', it fails with 'error near END.' In below format, fails with:
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 '' at line 64
#Begin statement
CREATE EVENT airshipmentsnotinlong
ON SCHEDULE every 1 HOUR
ON COMPLETION PRESERVE
DO
BEGIN
INSERT into WORKORDERS
(
id
,client_id
,method
,carrier_id
,carrier
,username
,password
,blnumber
,containernumber
,bookingnum
,adddate
,moddate
,isdone
)
SELECT
DISTINCT 'null' as ID
,cs.customer_id as client_id
,'justin' as method
,cs.carrier_id
,c.scac
,'' as user
,'' as pass
,cs.blnumber
,cs.container
,'' as book
,now() as adate
,now() as modate
,'0' as done
FROM CUSTOMERSHIPMENTS CS
LEFT JOIN
SHIPMENTS S
ON
cs.container = s.containernumber
and cs.blnumber = s.blnumber
LEFT JOIN
CARRIERS C
ON
cs.carrier_id = c.id
WHERE
cs.hostcompany_id = cs.company_id
and cs.container like '.air%'
and cs.isactive = 1
and cs.hostcompany_id = company_id
and cs.carrier_id in (176,180,222,224,226,227,228,261,271,292,297)
and cs.date > NOW() - INTERVAL 3 MONTH
and cs.blnumber <> ''
#and s.status = ''
and cs.blnumber not in
(
SELECT
blnumber
FROM
workorder_log
WHERE
cdate > now()-interval 75 minute
)
;
END
Your understanding to the contrary notwithstanding, you need to set the delimiter. Do this.
DELIMITER $$
CREATE EVENT airshipmentsnotinlong
ON SCHEDULE every 1 HOUR
ON COMPLETION PRESERVE
DO
BEGIN
...your event's INSERT statement here including the closing semicolon ...
END $$
DELIMITER ;
In PHPMyAdmin, instead of wrapping your definition in DELIMITER $$ / DELIMITER ; you set the delimiter to something besides ; in the box right below the query. You then terminate your definition with that same delimiter, as I have shown in END$$.
The error message you're getting is protesting the missing END, which MySQL doesn't see because it comes after the closing delimiter.