I am a beginner to MySQL and have written some basic MySQL stored procedures but cannot see what is wrong with the code below, I am getting the 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 '' at line 9"
CREATE PROCEDURE CA_Daily
(
DateTime_Start datetime
)
BEGIN
SET DateTime_Start = DATE_FORMAT((CURDATE() - INTERVAL 1 DAY ) ,'%Y%-%m-%d %T');
END;
There is a lot more to the original code which when I run it just as a piece of script works fine its just when I try to create it as a stored procedure it fails, I have it down to how I am dealing with the parameters but not sure what I am doing wrong....if not everything.
Any help would be great.
Please try this:
DELIMITER $$
CREATE PROCEDURE CA_Daily1
(
DateTime_Start datetime
)
BEGIN
SET DateTime_Start = DATE_FORMAT((CURDATE() - INTERVAL 1 DAY ) ,'%Y%-%m-%d %T');
END$$
You should use the STR_TO_DATE conversion in your SET statement:
Example:
STR_TO_DATE('8/21/2017 12:58 PM', '%c/%e/%Y %H:%i')
Related
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;
I tried running this in phpmyadmin...
It turned out something is not right.
I cannot figure out what is wrong here.
DELIMITER ;
create definer=proiect_wd_user#localhost FUNCTION
f_suma(id_s int, price_f double,product_code_n char(255),quantity_b int)
returns double
BEGIN
select price_f into #price_f
from orders_details WHERE (id=id_s)
select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
end ;
Error:
MySQL said: Documentation
#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 'select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #su' at line 7
There are various problems here with your delimiters. See Working with Stored Procedures in the MySQL documentation.
DROP FUNCTION IF EXISTS f_suma;
DELIMITER //
CREATE DEFINER=proiect_wd_user#localhost
FUNCTION f_suma(id_s int,price_f double,product_code_n char(255),quantity_b int)
RETURNS double
BEGIN
SELECT price_f INTO #price_f FROM orders_details WHERE (id=id_s);
SELECT quantity_b INTO #quantity_b FROM orders_details WHERE (id=id_s);
SET #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
END //
DELIMITER ;
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 ;
I am trying to create a query to return the number of working days in between 2 days but while trying to create a function it is giving below error.
ERROR 1064 (42000) at line 1: 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 5
Kindly help me on this, this is the first ever MySQL function I wrote. Currently, I am unable to figure it out where I did wrong and also if there is any scope for improvement let me know.
CREATE FUNCTION getNumOfDays (order_num_input INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE total_days INT;
SET total_days = 0;
/* To get the id, end_date, actual_end_date and getting num of days between start date and end date of the table based on provided order number */
SELECT case when datediff(end_date, start_date) = 0 then 1 else datediff(end_date, start_date) end INTO total_days
FROM order
WHERE order_num = order_num_input;
RETURN total_days;
END
I have some stored procedures and a trigger that work great in MySQL 5.5.8 but for some reason don't work in 5.1. The error descriptions aren't enough for me to figure out the problem. Here is the code and the errors.
CREATE PROCEDURE `cg_getMatchingContent`(
MatchTerm VARCHAR(255),
MaxResults INT)
BEGIN
SELECT * FROM (
SELECT t.*, INSTR(t.`Title`,MatchTerm) as Pos
FROM cg_content t ) c
WHERE Pos>0 ORDER BY Pos LIMIT 0, MaxResults;
END
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 'MaxResults' at line 8
DELIMITER ;;
CREATE TRIGGER `cg`.`cg_content_UrlDup_ConstTrig`
BEFORE INSERT ON `cg`.`cg_content`
FOR EACH ROW
Begin
DECLARE errorString VARCHAR(500);
DECLARE insert_error CONDITION FOR SQLSTATE '99001';
IF new.Url = '' THEN
SET errorString = CONCAT('Url cannot be blank
Title: ' , new.Title);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
END if;
IF Exists(SELECT id FROM cg.cg_content WHERE Url=new.Url) THEN
SET errorString = CONCAT('Url is not unique
Title: ' , new.Title , '
Url: ' + new.Url);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
End if;
End ;;
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 'insert_error
SET MESSAGE_TEXT=errorString;END if;IF ' at line 10
From the docs:
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
5.1 does not support variables in LIMIT and OFFSET.
The second one is easy to figure, hard to fix. SIGNAL and RESIGNAL commands were introduced in MySQL 5.5. You can't convert it easily to 5.1. One way to do it, would be to run a query that errors. For example a SELECT from a non-existent table.