Use IN param in WHERE clause inside MySQL stored procedure - mysql

I am using MySQL with HeidiSQL and I want to create a stored procedure that takes one String param and returns a table. This is what I have tried:
CREATE PROCEDURE sp(IN in_param VARCHAR(50))
BEGIN
SELECT * FROM fact_table ft
WHERE ft.param = #in_param
END
And I called it like this:
CALL sp('string_param');
The procedure returns an empty table, as #in_param is somehow NULL inside the SP.
I also like this: WHERE ft.param = in_param, but the I got an error when I ran it, saying SQL Error (1054): Unknown column 'in_param' in 'where clause'
Can someone tell me what I'm doing wrong?
PS:
I tried creating it by hand, and also using Heidi's Create new -> Stored routine wizard

I finally found out a solution that works:
CREATE PROCEDURE sp(IN in_param VARCHAR(50))
BEGIN
DECLARE declared_in_param CHAR(50);
SET declared_in_param = in_param;
SELECT * FROM fact_table ft
WHERE ft.param = declared_in_param;
END
So, the idea was to declare and set a new variable to that IN param, and use that declared variable inside the WHERE clause of the SELECT statement.
I haven't had time to research WHY this works, but I will

Related

Stored function in MYSQL

If I have a TABLE named MyTable which has columns say C1(type date) and C2 (type character) I want to create a stored function that takes an input and the input should always belong to C1, and the output of the stored function should be the corresponding element in C2. I have tried to do it using the 'select' statement followed by 'where' clause inside the stored function but was not able to achieve it. Is there any other way to accomplish this task.
DELIMITER $$
CREATE FUNCTION `MyFunction`
(`Date` datetime)
RETURNS char(10)
BEGIN
DECLARE MyVariable char(10)
SELECT MyVariable = `C2`
FROM MyTable
WHERE `Date` = `C1`; RETURN MyVariable;
END$$
DELIMITER ;
But this keeps giving me ERROR CODE: 1064
At first glance, I see a syntax error:
...
BEGIN
DECLARE MyVariable char(10) <-- needs a semicolon here
SELECT MyVariable = `C2`
...
Every statement within the body of your routine must end with a semicolon. See examples of DECLARE in this manual page: https://dev.mysql.com/doc/refman/8.0/en/local-variable-scope.html
It should be like this:
...
BEGIN
DECLARE MyVariable char(10);
SELECT MyVariable = `C2`
...
Re your comment:
Error 1415 means "cannot return a result set". Your stored function is doing a SELECT without putting the result into your declared local variable using an INTO keyword.
You appear to be trying to set the value of MyVariable using = but that's just making a comparison. It doesn't assign anything to MyVariable.
Without using INTO to assign the variable, your SELECT statement is by default returning a result set. This is allowed in a stored procedure, but not in a stored function. A stored function must return a single scalar value, not a result set.
...
BEGIN
DECLARE MyVariable char(10);
SELECT `C2` INTO MyVariable
FROM MyTable
WHERE `Date` = `C1`;
RETURN MyVariable;
END
P.S.: I edited your question to replace the term "user-defined function" with "stored function". These are two different things in MySQL. You are writing a stored function.
In MySQL, they use the term user-defined function (UDF) for a function you implement in C/C++ code and compile into the MySQL server. It's less common for developers to write this type of extension.

Mysql procedure, using parameter in where clause

I made this procedure from phpmyadmin, but it don't works, I replaced the last word from record_id to a specified string id and worked, but when I use the parameter not working.
DROP PROCEDURE `prcd_update_record`;
CREATE DEFINER=`root`#`localhost`
PROCEDURE `prcd_update_record`(
IN `talep_id` VARCHAR(24),
IN `vall` INT(10)
)
NOT DETERMINISTIC
MODIFIES SQL DATA SQL
SECURITY INVOKER
UPDATE `talep_malzeme`
SET `kalan_miktar` = vall
WHERE `talep_malzeme`.`id` = talep_id;
The I execute it like this:
SET #p0='33'; SET #p1='57fb7911ea91e9efa'; CALL `prcd_update_record`(#p0, #p1);
DROP PROCEDURE IF EXISTS `prcd_sevk_toplam`;
create procedure prcd_sevk_toplam(talep_id int, vall VARCHAR(255))
BEGIN
UPDATE `talep_malzeme` SET `kalan_miktar` = vall WHERE `talep_malzeme`.`id` = talep_id;
END;
Hope this will help you.
Looks like you has wrong parameter order, try
CALL `prcd_sevk_toplam`(#p1, #p0);
You should provide proper value to your parameter per your parameter definition. Your procedure accepts parameter as below
PROCEDURE `prcd_sevk_toplam`(
IN `talep_id` VARCHAR(24),
IN `vall` INT(10)
And you are setting both of them to varchar. That could be the issue here. You should set them as
SET #p0=33;
SET #p1='57fb7911ea91e9efa';
CALL `prcd_sevk_toplam`(#p1, #p0);

Table Parameter in MySQL Function

Basically I am trying to refer to my table in mysql function, so that in my query I can say "from x" as in x is a parameter of the function, so that someone can put in the table they want the function to run on.
CREATE DEFINER=`root`#`localhost` FUNCTION `somefunction`(t varchar(8), num integer) RETURNS int(8)
BEGIN
DECLARE result integer(12);
DECLARE test varchar(12);
SET result = 0;
SET test = t;
select integer * 5 INTO result from x;
return result;
END
Basically when I do somefunction(thisisthetableiwant, 5) I get an error saying that it cannot find 'test in field list' so it isn't setting the table to what I put in the parameter, currently I have the part "from x" hardcoded with the table I want and it works but I need to make it so I can have a parameter incase I need to use the function on another table

Calling a stored procedure within an IF statement MySQL

Does anybody know if this is allowed?
IF CALL GET_RIGHT_NODE(edge) = 15
THEN
SELECT "IT WORKS";
I'm getting an error on this syntax, is it possible any other way?
The return values from stored procedures should be captured in OUT paramters (whereas those from user defined functions can be captured as #returnValue = function()).
So, your GET_RIGHT_NODE should take an OUT parameter and set it to the return value.
CREATE PROCEDURE GET_RIGHT_NODE
(
#edge INT,
#returnValue INT OUTPUT
)
AS
-- Definition of the proc.
then you would call the procedure as follows:
DECLARE #returnValue INT
CALL GET_RIGHT_NODE(#edge, #returnValue)
IF (#returnValue = 15)
THEN
SELECT 'IT WORKS'

Weird issue with a stored procedure in MySQL

I need to add a new stored procedure on our company's MySQL server. Since it's just slightly different, I used an already existing one, added the additional field and changed the name of the procedure. The weird thing now is that when I want to execute the statement, it returns:
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 3
reffering to the 0 in this line: SET #update_id := 0; What makes it weird is, that I queried that stored procedure by using SHOW CREATE PROCEDURE . It's saved in our database and is working fine. I just can't use it as a new stored procedure (no matter if I try to apply it to the new test database or if I use it on the existing database by giving it a new name).
I searched the internet for a solution. Unfortunately to no avail. I even set up a new database with a new table and some demo values where I tried to execute the original, unaltered stored procedure. It returns the exact same error.
Here's the currently used and working stored procedure I'm talking about:
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
SET #update_id := 0;
UPDATE customer_shop SET state = 1, id = (SELECT #update_id := id), instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment WHERE tariff=Itariff AND state = 0 LIMIT 1;
SELECT * FROM customer_shop WHERE id = #update_id;
END
I hope you guys can help me as I am completely out of ideas what's wrong. :/
Regards, Mark
You need to define an alternative command delimiter, as MySQL currently thinks your CREATE PROCEDURE command ends at the first ; it encounters (on line 3, after the 0), which would be a syntax error as it's after a BEGIN but before the corresponding END:
DELIMITER ;; -- or anything else you like
CREATE PROCEDURE
...
END;; -- use the new delimiter you chose above here
DELIMITER ; -- reset to normal
MySQL stored procedures do not use ":=" for value assignment, just use "=".
Also don't think "id = (SELECT #update_id := id)" is acceptable. Here's an alternative solution (untested):
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
select id into #update_id from customer_shop WHERE tariff=Itariff AND state = 0 LIMIT 1;
UPDATE customer_shop SET state = 1, instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment where id = #update_id;
SELECT * FROM customer_shop WHERE id = #update_id;
END
You may also want to put error handlers in case there's no matching row to be edited.