function MySQL does not exist - mysql

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.

Related

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 to put output value in mysql function and return it

I'm having some trouble with the following problem:
delimiter //
create function geefPrijsVoorVlucht(FID INT, CID INT)
RETURNS INT(11)
begin
SELECT CurrPrice
into #prijs
FROM stats
where FlightID = FID and ClassID = CID;
return #prijs;
end //
delimiter ;
call geefPrijsVoorVlucht(652, 2);
It doesn't produce a return value, nor an error which I can base myself on.
I'm probebly assigning and returning the values the wrong way.
Btw I'm very new to Mysql, so please don't shoot me!
Can anywone see whatn I'm doing wrong?
Thanks!

SQL - Make a function that return lines of a table

I'm using Mysql and I'm trying to make a function that is able to return me all of the lines of a table if the id is ok.
that's my code :
CREATE OR REPLACE FUNCTION lesPieces(varchar)
RETURNS TABLE my_table
DECLARE
ligne piece%ROWTYPE;
BEGIN
FOR ligne IN
SELECT *
FROM piece;
LOOP
RETURN NEXT ligne;
END LOOP;
RETURN
END;
$$ LANGUAGE plpgsql;
But Mysql returns me :
You have an error in your SQL syntax near :
'FUNCTION lesPieces(varchar)
RETURNS TABLE my_table
DECLARE
ligne piece%ROWTY' at line 1
Have you got an idea ?
------------------------------------------------------------------------------------------------------------------------
EDIT :
Thanks to #Slasko for this help :
You sure REPLACE is command ? should it be something like DROP FUNCTION IF EXISTS [function_name]; DELIMITER $$ CREATE FUNCTION [function_name(function_parameter)] ?
So I just removed the "OR REPLACE" but now the error is :
You have an error in your SQL syntax near : 'varchar) RETURNS SETOF piece AS' at line 1
This create function has many issues. For e-g RETURNS can have possible values {STRING|INTEGER|REAL|DECIMAL} as per http://dev.mysql.com/doc/refman/5.7/en/create-function-udf.html
Please refer below link for examples
http://sql-info.de/mysql/examples/create-function-examples.html

Mysql error: Not allowed to return a result set from a function

I am trying to have a conditional change in a parameter for update statement.
I am getting the following error when I try the following function
/home/y/bin/mysql -u root < testpri.sql > out
ERROR 1415 (0A000) at line 4: Not allowed to return a result set from a function
Contents of testpri.sql are as follows:
use `zestdb`;
DROP FUNCTION IF EXISTS UPDATEPASSWD;
DELIMITER //
CREATE FUNCTION UPDATEPASSWD(n INT) RETURNS varchar(255) DETERMINISTIC
BEGIN
DECLARE mypasswd varchar(255);
IF (n = 1) THEN
SET mypasswd = '12ccc1e5c3c9203af7752f937fca4ea6263f07a5';
SELECT 'n is 1' AS ' ';
ELSE
SET mypasswd = '1a7bc371cc108075cf8115918547c3019bf97e5d';
SELECT 'n is 0' AS ' ';
END IF;>
SELECT CONCAT('mypasswd is ', mypasswd) AS ' ';
RETURN mypasswd;
END //
DELIMITER ;
CALL UPDATEPASSWD(0);
What am I missing?
I think it's actually your debugging SELECT calls.
From the docs:
Statements that return a result set can be used within a stored procedure but not within a stored function. This prohibition includes SELECT statements that do not have an INTO var_list clause...
I arrived in search of answers to the same question, and found another way to work around the issue, so that I can use the SELECT statement that is the heart and soul of the MySQL function that elicited the warning.
Consider the following snippet.
SET intNMatches = ( SELECT COUNT(*) ...
SET coerces the SELECT statement to return its one and only column, a row count, into intNMatches, a local variable cast to BIGINT. Since it contains trade secrets, I can't show the rest of the query. Suffice it to say that the query installs without causing the MySQL engine to issue a warning.

Function in postgresql wit ref cursor

Im trying to migrate an oracle procedure to a postgresql function. Here's the function in postgres:
CREATE OR REPLACE FUNCTION tibrptsassure.call_reasons(i_start_date date, i_end_date date, i_intnbr character varying, i_intmodnbr character varying, oc_ref_cursor refcursor)
RETURNS refcursor AS
$BODY$
BEGIN
OPEN oc_ref_cursor FOR
SELECT COUNT(1),INTERACTION_NBR,INTERACTION_ID,INTERACTION_MODULE_NBR,CREATED_BY
FROM tibrptsassure.d_tcare_interaction , tibrptsassure.d_calendar d
WHERE INTERACTION_ID = i_intnbr
AND INTERACTION_MODULE_NBR = i_intmodnbr AND INTERACTION_DATE BETWEEN i_start_date AND i_end_date
AND INTERACTION_DATE BETWEEN d.week_start_date AND d.week_end_date
GROUP BY INTERACTION_NBR;
return oc_ref_cursor;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
But, while executing this function, I dint get any output. Got a message: Query result with 1 row discarded.
begin;
select tibrptsassure.sampleproc_call('10-Feb-2010','31-Dec-2013','30681','Bypass_IDV','funccursor');
FETCH ALL IN "funccursor" ;
COMMIT;
Whats wrong in the query?
This is an issue with pgAdmin and multi-statement transactions. Use psql instead.
Basically pgAdmin doesn't know what to do at that point and so it discards the row and you can't with cursors outside such an environment.