Error creating stored procedure when creating counter - mysql

When I run the following SQL
CREATE
PROCEDURE `Calendar`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS `Cal` (
`month` INT(11) ,
`year` INT(11)
);
SET #counter := -1;
WHILE (DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL #counter MONTH))) < DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL 12 MONTH)))) DO
INSERT INTO Cal SELECT DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL #counter:=#counter + 1 MONTH),'%m'),DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL #counter + 1 MONTH),'%Y');
END WHILE;
END
to create and stored procedure, I get the following error
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 7
Any idea to fix the syntax error? Thanks in advance.

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

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 ;

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.

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