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
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 have been trying to write a function that retrieves an employees pay rate on a specific date; however I keep running into a syntax error which I cannot figure out.
Here is my code:
create function rate_on_date(staff_id INT, given_date DATE)
returns float
DETERMINISTIC
BEGIN
DECLARE hour_rate float;
SELECT DISTINCT salarygrade.HOURLYRATE STATUS INTO hour_rate
FROM salarygrade JOIN staffongrade
ON salarygrade.GRADE = staffongrade.GRADE
WHERE staff_id = staffongrade.STAFFNO
AND given_date BETWEEN staffongrade.STARTDATE AND staffongrade.FINISHDATE;
RETURN hour_rate;
END //
Here is the error message:
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 5
CREATE FUNCTION f_TP20avg (p_date date, p_symbol varchar(10)) RETURNS Decimal
BEGIN
DECLARE v_TP20avg Decimal(12,2);
SELECT avg(x.TypicalPrice) into v_TP20avg
FROM (SELECT TypicalPrice
FROM StockData
WHERE symbol = p_symbol
AND PriceDate <= p_date
ORDER BY PriceDate DESC LIMIT 20) x;
RETURN v_TP20avg;
END;
I am getting following error while trying to create function in MariaDB. I am not able to figure out what's wrong with syntax.
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 3
please help.
I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)
I have a working module in Magento that is modeled after some custom code we use outside of our install. This module currently add 5 tables to the database to store info and I have extended the Admin to CRUD the info. The ultimate goal here is to move the majority of this custom programming into Magento.
Currently our custom code sits outside Magento and hits a separate database. This database has those same 5 tables, a stored procedure, and 4 functions. What I would like to do now is move the stored procedures and the functions into Magento's database and change the custom code to call all of it's data from Magento's db. However, I can't seem to figure out how the "CREATE FUNCTION" call should be set up for Magento to execute it properly.
The SQL I am using is:
DROP FUNCTION IF EXISTS {$this->getTable('fn_Get_HardinessZone')};
CREATE FUNCTION {$this->getTable('fn_Get_HardinessZone')}(IN ZipCode varchar()) RETURNS integer AS
DECLARE Result integer;
BEGIN
SELECT MAX(Zone) into Result
FROM AMI_zones
WHERE (Hfzip <= LEFT(ZipCode, 5)) AND (Htzip >= LEFT(ZipCode, 5));
if Result is null or Result < 1 or (Result > 11 and Result <> 99) Then
/*if the left most character is alpha, then set the zone to 98 for Canada*/
if Left(zipCode, 1) >= 'A' and LEFT(zipcode,1) <= 'Z' THEN
set result = 98;
else
set Result = 99;
End if;
END if;
RETURN Result;
END;
But this always generates the following error:
SQLSTATE[42000]: Syntax error or access violation: 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 'IN ZipCode varchar()) RETURNS'
So what is the proper way to format a SQL call to be run in a module's install/update script to insert a function or stored procedure into Maganeto's database?
The problem is with your SQL statement:
You have an error in your SQL syntax; check the manual ...
for the right syntax to use near
'IN ZipCode varchar()) RETURNS'
I would recommend running the SQL through PhpMyAdmin or on the command line until you get it right, then run it through Magento. This man page describes the syntax of CREATE FUNCTION: http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html. When testing stored procedures /functions in the mysql client (or PhpMyAdmin) be sure to change the delimiter so that the semicolons in your function body are interpreted correctly.
The below SQL worked for me. The things I changed from your original statement are:
IN is not allowed in function declarations (IN ZipCode varchar())
I was required to explicitly state length of the varchar
The DECLARE belongs inside the function
I am guessing that your function is DETERMINISTIC, meaning it will always produce the same results for the same input parameters. If this is not the case, remove DETERMINISTIC from the RETURNS line
Give this a shot:
DROP FUNCTION IF EXISTS {$this->getTable('fn_Get_HardinessZone')};
CREATE FUNCTION {$this->getTable('fn_Get_HardinessZone')} (ZipCode VARCHAR(15))
RETURNS INTEGER DETERMINISTIC
BEGIN
DECLARE result INTEGER;
SELECT MAX(Zone) INTO result
FROM AMI_zones
WHERE (Hfzip <= LEFT(ZipCode, 5)) AND (Htzip >= LEFT(ZipCode, 5));
IF result IS NULL OR result < 1 OR (result > 11 AND result <> 99) THEN
/* if the left most character is alpha, then set the zone to 98 for Canada */
IF LEFT(ZipCode, 1) >= 'A' AND LEFT(ZipCode, 1) <= 'Z' THEN
SET result = 98;
ELSE
SET result = 99;
END IF;
END IF;
RETURN result;
END;