phpMyAdmin can't find syntax for Create EVENT - mysql

-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.

Related

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 ;

MySQL - CREATE DEFINER syntax error

I am trying to update a stored function in our MySQL database. The update is to be released to multiple devices so I am doing it through an update.sql file.
Here is the function
DROP FUNCTION `STAFF_MPT`;
CREATE DEFINER=`jelena`#`%` FUNCTION `STAFF_MPT`(`par_stocktake_staff_id` INT) RETURNS DECIMAL(20,0) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN
DECLARE proc_total INT;
DECLARE proc_time INT;
SET proc_total = (SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_staff_id = par_stocktake_staff_id);
SET proc_time = (SELECT TIMESTAMPDIFF( SECOND , MIN( scan_date ) , MAX( scan_date ) ) AS area_time
FROM stocktake_scans
WHERE stocktake_staff_id = par_stocktake_staff_id
);
RETURN (proc_total/proc_time)*3600;
END
It was just reported to me by the test team that the report that uses this function did not generate properly. I tried to run the code in PMA SQL query window and got the following:
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 3
Can someone tell me what am I missing? According to this, line 3 is empty, so how could it possibly have a syntax error?
As of MySQL docs:
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.
To redefine the mysql delimiter, use the delimiter command.
#juergen_d hinted in the comment: you have to define your procedure with a delimiter:
DROP FUNCTION `STAFF_MPT`;
delimiter ||
CREATE DEFINER=`jelena`#`%` FUNCTION `STAFF_MPT`(`par_stocktake_staff_id` INT) RETURNS DECIMAL(20,0) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN
DECLARE proc_total INT;
DECLARE proc_time INT;
SET proc_total = (SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_staff_id = par_stocktake_staff_id);
SET proc_time = (SELECT TIMESTAMPDIFF( SECOND , MIN( scan_date ) , MAX( scan_date ) ) AS area_time
FROM stocktake_scans
WHERE stocktake_staff_id = par_stocktake_staff_id
);
RETURN (proc_total/proc_time)*3600;
END
||
delimiter ;

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 ;

Stored procedure MYSQL error

Having a few issues with running the below procedure over PHPMyAdmin, receiving 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 '' at line 6"
The problem seems to be with the IF, the update syntax works on its own, the select within the if statement works on its own.
Any ideas??
CREATE PROCEDURE Get_SessionCookie(
sessionID varchar(50),
cookieID varchar(50)
)
IF (SELECT 1 = 1 FROM `SessionCookie` WHERE SessionID = sessionID AND CookieID = cookieID AND SessionExpiry < NOW())
UPDATE SessionCookie
SET SessionExpiry = NOW() + INTERVAL 60 MINUTE
WHERE SessionID = sessionID AND CookieID = cookieID;
SELECT 'True' FROM SessionCookie;
ELSE
SELECT 'False' FROM SessionCookie;
One major problem is that parameters to the stored procedure have the same names as columns. You should always prefix variable names with something. Your specific problem, I think, is that you want an exists in the if and a delimeter statement. I think this is closer to what you want:
delimiter $$
CREATE PROCEDURE Get_SessionCookie (
var_sessionID varchar(50),
var_cookieID varchar(50)
)
BEGIN
IF exists (SELECT 1
FROM `SessionCookie`
WHERE SessionID = var_sessionID AND
CookieID = var_cookieID AND
SessionExpiry < NOW()
)
UPDATE SessionCookie
SET SessionExpiry = NOW() + INTERVAL 60 MINUTE
WHERE SessionID = var_sessionID AND CookieID = var_cookieID;
SELECT 'True' FROM SessionCookie;
ELSE SELECT 'False' FROM SessionCookie;
END IF
END$$

How to set up a WHILE loop with IF statement in MySQL?

I would like to create a stored routine for MySQL that figures out the number of business or working days for a month (Working Days are Monday thru Friday).
It's a syntax error however I don't know what the syntax error is. All it tells me is:
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 'WHILE(#daycount < #totaldays) DO IF (WEEKDAY(#checkweekday) < 6)
THEN ' at line 2
My Syntax Error is in the following:
WHILE(#daycount < #totaldays) DO
IF (WEEKDAY(#checkweekday) < 6) THEN
My Code:
SELECT MONTH(CURDATE()) INTO #curmonth;
SELECT MONTHNAME(CURDATE()) INTO #curmonthname;
SELECT DAY(LAST_DAY(CURDATE())) INTO #totaldays;
SELECT FIRST_DAY(CURDATE()) INTO #checkweekday;
SELECT DAY(#checkweekday) INTO #checkday;
SET #daycount = 0;
SET #workdays = 0;
BEGIN
WHILE(#daycount < #totaldays) DO
IF (WEEKDAY(#checkweekday) < 6) THEN
SET #workdays = #workdays+1;
END IF;
SET #daycount = #daycount+1;
SELECT ADDDATE('#checkweekday', INTERVAL 1 DAY) INTO #checkweekday;
END WHILE;
END;
SELECT #workdays;
Is someone able to assist?
UPDATE
I receive the same error with the following bit of code so it probably has something to do with this:
SET #workdays = 0;
IF (WEEKDAY('2013-06-13') < 6) THEN
SET #workdays = #workdays+1;
END IF;
SELECT #workdays;
I have discovered that you cannot have conditionals outside of the stored procedure in mysql. This is why the syntax error. As soon as I put the code that I needed between
BEGIN
SELECT MONTH(CURDATE()) INTO #curmonth;
SELECT MONTHNAME(CURDATE()) INTO #curmonthname;
SELECT DAY(LAST_DAY(CURDATE())) INTO #totaldays;
SELECT FIRST_DAY(CURDATE()) INTO #checkweekday;
SELECT DAY(#checkweekday) INTO #checkday;
SET #daycount = 0;
SET #workdays = 0;
WHILE(#daycount < #totaldays) DO
IF (WEEKDAY(#checkweekday) < 5) THEN
SET #workdays = #workdays+1;
END IF;
SET #daycount = #daycount+1;
SELECT ADDDATE(#checkweekday, INTERVAL 1 DAY) INTO #checkweekday;
END WHILE;
END
Just for others:
If you are not sure how to create a routine in phpmyadmin you can put this in the SQL query
delimiter ;;
drop procedure if exists test2;;
create procedure test2()
begin
select ‘Hello World’;
end
;;
Run the query. This will create a stored procedure or stored routine named test2. Now go to the routines tab and edit the stored procedure to be what you want. I also suggest reading http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/ if you are beginning with stored procedures.
The first_day function you need is:
How to get first day of every corresponding month in mysql?
Showing the Procedure is working
Simply add the following line below END WHILE and above END
SELECT #curmonth,#curmonthname,#totaldays,#daycount,#workdays,#checkweekday,#checkday;
Then use the following code in the SQL Query Window.
call test2 /* or whatever you changed the name of the stored procedure to */
NOTE: If you use this please keep in mind that this code does not take in to account nationally observed holidays (or any holidays for that matter).