Can't declare a variable in a MySQL Function - mysql

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

Related

MySQL creating procedure getting error I cannot figure out why

I want to create a procedure for a MySql database but I get this error below and after 2h of research I still cannot figure out why.
The code is the following :
delimiter //
create procedure mitarbeiter_projekt (proj_name varchar(20), mitarb_name varchar(20))
begin
declare pruef_id int;
declare new_id int;
declare mitarb_id int;
select count(id) from t_proj where name = proj_name into pruef_id;
select id from t_ma_dt where name = mitarb_name into mitarb_id;
if pruef_id = 0 then
select max(id) + 1 from t_proj into new_id;
insert into t_proj (id, name) values (new_id, proj_name);
insert into t_ma_proj (ma_id, proj_id )values (mitarb_id, new_id);
else
select id from t_proj where name = proj_name into new_id;
insert into t_ma_proj (ma_id, proj_id) values (mitarb_id, new_id);
end //
"ERROR 1064 (42000): 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 15"
all of the above tables I used exist in my database.
Thankful for any help!

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

mariadb - what wrong in this create function statement

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.

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)