Table Parameter in MySQL Function - mysql

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

Related

I Getting error when run this below query : RETURN statements in scalar valued functions must include an argument

I am getting this error message
RETURN statements in scalar valued functions must include an argument
when run this query:
create function gender(#gender nvarchar(40))
returns nvarchar(40)
as
begin
(select name,cast(DOB as date) as DOB from datenames where gender = #gender)
return
end
The write way to create a function in mysql for your example is as follows:
DELIMITER \\
create function gender(Igender nvarchar(40))
returns nvarchar(40)
begin
DECLARE customerLevel NVARCHAR(40);
IF EXISTS (select name,cast(DOB as date) as DOB from datenames where gender = Igender) THEN
SET customerLevel = 'SOMETHING1';
ELSE
SET customerLevel = 'SOMETHING2';
END IF;
RETURN (customerLevel);
end
No need to as
No need to # before input
You need to return something.
Don't forget to use DELIMITER.
If you use phpmyadmin and has problem with nvarchar read this post: Unrecognize data type nvarchar in database or simply change it to to varchar.

Unable to pass table name into MySQL function

As part of an exercise, I am trying to write a MySQL function that accepts a table name as an argument and returns the count of records from that table. My function looks like -
DELIMITER //
create function recordCount(tname text)
returns int deterministic
begin
DECLARE rc int;
select count(*)
into rc
from tname;
return rc;
end;
//DELIMITER
However, when I call this function like -
select recordCount('avengers');
I get the following error:
Error Code: 1146. Table 'db.tname' doesn't exist 0.000 sec
I don't understand why the function isn't looking for the table 'avengers'. I am unable to figure out what I am doing wrong, please help.

Use IN param in WHERE clause inside MySQL stored procedure

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

Mysql calculate the average of a function's parameter

Here is a function to describe my question:
CREATE DEFINER=`root`#`localhost` FUNCTION `Demo`(`data` Integer) RETURNS int(11)
BEGIN
declare result integer;
set result = avg(`data`);
return result;
RETURN 1;
END
And the parameter data is a whole column from other select result, just looks like:
10
12
15
...
I want to use this function to calculate the average of the data, but it just shows:
Error Code: 1111. Invalid use of group function
It seems that was a wrong way to use the avg function, and other function like count() has the same problem too, but I can't find a way to achieve my purpose, is there a way to do that?
It makes no sense to calculate the average of a single value. The average of a single value is the same as the min and the same as the max, and these are all equal to the single value itself.
It's hard to tell what you intend this function to do. If you want to use it in a query, you can calculate the average of its result as you use it on each row of a query:
SELECT AVG(Demo(data)) FROM MyTable;
Or you can use the function to query some table and return the average of many rows. But then you don't need to pass any function argument.
CREATE FUNCTION Demo() RETURNS INT
BEGIN
DECLARE result INT;
SET result = (SELECT AVG(`data`) FROM MyTable);
RETURN result;
END
You would have to pass it an array.
CREATE DEFINER=`root`#`localhost` FUNCTION `Demo`(a_array IN my_integer_array) RETURNS int(11)
BEGIN
FOR i IN a_array.first..a_array.last LOOP
set result = a_array(i);
return result;
END LOOP;
RETURN 1;
END

Compare string with select statement mysql

i have to make a function in mysql that returns 0 or 1.
DELIMITER //
drop function compruebacuenta//
CREATE FUNCTION compruebacuenta(ncliente INT) RETURNS INT
BEGIN
DECLARE fin int default 0;
DECLARE cuenta varchar(30);
DECLARE cliente varchar(30);
DECLARE cuentacliente varchar(30);
select codigo_cliente FROM clientes where codigo_cliente=ncliente into cuentacliente;
RETURN fin;
END;
//
user gives a number when calling the function i.e select function(1);
what i do then is get the cod for a client with that given code (I know a bit redundant , but whatever); and store it in a variable called cuentacliente;
What i need to do now is get a query like this one
select cod_cliente from cuentas;
store that in another and I want to know how can I compare lets say a 10 , to see if there's a 10 in the resulting query.
I'm thinking that perhaps i need a cursor that fetches each row and compare that with what's in cuentacliente.