problem with mysql function with parameter and select - mysql

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

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.

How can I fix a MySQL Error, Not allowed to return a result set from a function

CREATE FUNCTION `ConvertDate`(StringDate varchar(15))
RETURNS datetime
BEGIN
declare CDATE varchar(10);
SET CDATE = StringDate;
select str_to_date(CDATE,'%Y%m%d %h%i');
RETURN CDATE;
END
I get an error:
Error Code: 1415 Not allowed to return a result set from a function
How can I fix this Error?
Your problem is that your SELECT statement has no INTO clause, so it attempts to return the result set from the function. You could alter that to:
SELECT STR_TO_DATE(CDATE,'%Y%m%d %h%i') INTO CDATE;
But you might as well do all the computation in the RETURN statement:
CREATE FUNCTION `ConvertDate2`(StringDate varchar(15))
RETURNS datetime
RETURN STR_TO_DATE(StringDate,'%Y%m%d %h%i');
You should
Select INTO cdate...
or don't bother with the select and
set cdate = str_to_date..
BUT Unless this is a very simplified version of your actual function I don's see the point of the function.

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');

Fixing "No data - zero rows fetched, selected, or processed" warning

The below function works fine except it throws a warning 'No data - zero rows fetched, selected, or processed (errno. 1329)'. and since i start using this function with django there cant be any warning or error because it stop the whole process
any idea how to fix this?
DELIMITER $$
DROP FUNCTION IF EXISTS objtree_node_add $$
CREATE FUNCTION objtree_node_add(i_name VARCHAR(255), i_parent_id BIGINT, i_type_id BIGINT) RETURNS bigint(20)
BEGIN
DECLARE a_name VARCHAR(255);
IF NOT i_name RLIKE '^[a-zA-Z0-9_-]+$' THEN
RETURN -1;
END IF;
SELECT name INTO a_name FROM objtree_nodes WHERE parent_id = i_parent_id AND name = i_name;
IF NOT a_name IS NULL THEN
RETURN -5;
END IF;
...
I don't know where you read that there is no warnings filtering in Django. Django is just Python, so you can use the Python warnings module.
import warnings
warnings.filterwarnings("ignore", "No data .*")
import warnings
warnings.filterwarnings("ignore", "No data .*")