Unable to pass table name into MySQL function - mysql

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.

Related

problem with mysql function with parameter and select

I have a problem with a simple mysql function that only receives one parameter, perform a select with it and if it does not succeed I need it to return a default value, the problem is that when I execute the function I receive the following error:
Procedure execution failed
1054 - Unknown column 'test' in 'field list'
mysql code:
CREATE FUNCTION `fn_buscar_id`(pvalor VARCHAR(100)) RETURNS int(11)
BEGIN
DECLARE Pid INT;
select ID INTO Pid from PARAMETROS_GENERALES WHERE VALOR = pvalor||' ';
IF Pid is null THEN
SET Pid = 514;
END IF;
RETURN Pid;
END;
no matter what you enter, you get the same error, I know it is something simple but I win ... hehe thanks for your prompt response

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

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

function MySQL does not exist

I'm creating my first function, with the following code:
CREATE FUNCTION CTRLPRODUCAO.COMPARATOTAIS (AG INT, P INT, ANO INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
(...)
END
When I run the command, received the return of the Workbench: "0 row (s) affected". Is it ok?
When I run
SELECT CTRLPRODUCAO.COMPARATOTAIS (1, 9, 2011) AS TEST;
I get
"Error Code: 1305 FUNCTION CTRLPRODUCAO.COMPARATOTAIS does not exist"
What am I doing wrong?
You can't insert a . in your function name.
As far as i know, in MySQL, the . is interpeted as a kind of join and in your example, MySQL is looking for the function COMPARATOTAIS in CTRLPRODUCAO table.

Problem with my first MySQL Function. Getting one error after the other

So, I'm making my first MySQL function.
BEGIN
DECLARE ID INT;
SELECT
LandID INTO ID
FROM
Landen
WHERE
Landnaam = landnaam;
RETURN ID;
END
The parameter is a varchar.
So, let's say I got a record in Landen with the Landnaam 'Nederland'.
I run my function, give 'Nederland' as parameter.
But then it gives me to following error:
1172 - result consisted of more than one row
Which makes no sense at all. Cause when I give something as a parameter that is not in my database, like 'ASDASD', I get the same error.
And when limmiting the results with
LIMIT 1
It just always returns 1.
What am I doing wrong?
drop function if exists myfunc;
delimiter //
create function myfunc(str varchar(50))
returns int
reads sql data
begin
declare id int;
select landid into id from Landen where landnaam = str limit 1;
return id;
end//
delimiter ;
select myfunc('nederland');