mariadb - what wrong in this create function statement - function

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.

Related

DATEADD() function doesn't return the adding value

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;

Can't declare a variable in a MySQL Function

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

MYSQL function creation syntax error

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

mysql stored procedure declare error

I don't really get the error ...
i put the declare statement right after begin. i usually do a lot of MS SQL so i am not that practiced in mysql syntax. please help me out
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddEntry2`(IN `inDate` DATE, IN `inUser` INT, IN `inComment` TEXT, IN `inStart` INT, IN `inEnd` INT)
BEGIN
DECLARE commID UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT commID := MAX(id)
FROM dimcomment
WHERE comment = inComment;
INSERT INTO factentry (FKDate, FKUser, FKComment,FKStart,FKEnd,FKAbsence)
VALUES (
inDate,
inUser,
commID,
inStart,
inEnd,
1
);
END //
Error I get:
#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 'UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT c' at line 3
well i changed UNSIGNED to INT and Error Message changed:
#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 ':= MAX(id)
FROM dimcomment
' at line 5
Seems like i cannot set via SELECT VAR := VALUE ?

SQL If Statment / select into in a stored procedure

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)