mysql stored function - in varchar and return varchar - Error : 1172 - mysql

I could not figure out what went wrong with my stored function.
select batch as bach from test where mfg_code = 'BC-7A1-5' group by batch;
When I run the script above and I am able to get the number of rows as expected.
However I am not able with the similar script below:-
DELIMITER $$
DROP FUNCTION IF EXISTS `testdata1970_05`.`listbatch` $$
CREATE FUNCTION `listbatch`(mfgnum VARCHAR(24)) RETURNS VARCHAR(10)
BEGIN
DECLARE bach VARCHAR(10);
SELECT batch into bach FROM test WHERE mfg_code = mfgnum group by batch;
RETURN bach;
END $$
DELIMITER ;
And it returns error:
"ERROR 1172 (42000): Result consisted of more than one row"
And here is my query below:
select listbatch("BC-7A1-5");

Your error message is telling you that more than one row is being returned by your query, and the resultset cannot be stored in a simple variable, as the variable you defined can hold only one value!

Related

error trying to make function for counting friends

Error: There was an error while applying the SQL script to the database.
code:
USE `my_first_db`;
DROP function IF EXISTS `show_users_firends_count`;
DELIMITER $$
USE `my_first_db`$$
CREATE FUNCTION `show_users_firends_count` (ff int)
RETURNS int(11)
BEGIN
Select
count(user_friend.user_id + user_friend.friend_id)
From
user_friend
Where
user_friend.user_id = ff;
END$$
DELIMITER ;
You had it almost right.
A functions ahs to return something so you send the count back.
You should check the query anyway. your count doesn't look right. because you add useid and freind_id and count that, that is wrong. so check the query and change it. it now count all friends_id that are connected to a user_id
DROP function IF EXISTS `show_users_friends_count`;
DELIMITER $$
CREATE FUNCTION `show_users_friends_count` (ff int)
RETURNS int
DETERMINISTIC
BEGIN
Select
count(user_friend.friend_id) INTO #count
From
user_friend
Where
user_friend.user_id = ff;
RETURN #count;
END$$
DELIMITER ;

create simple function returning value from table

I'm trying to make very simple function returning lenght of text in cell of a table (in Workbench), but still getting error 1064 when trying to run it, though I think everything is good in commands below. Where is the problem?
DROP FUNCTION IF EXISTS how_long;
CREATE FUNCTION how_long(thing varchar(20)) RETURNS INT
BEGIN
DECLARE sth INT;
SELECT length(word) INTO sth FROM things WHERE name=thing;
RETURN sth;
END;
Ok, I already know. It was really stupid, but I tried to do it just by new sql table, not by >function and >right_click > create
try this instead
delimiter |
drop function if exists how_long|
create function how_long(thing varchar(20)) returns int
no sql
deterministic
begin
return length(thing);
end|
select how_long('hello')|
but why don't you just use the native function length() ?

error while creating mysql procedure having SYS_REFCURSOR as out param

I'm creating procedure which is having two parameters , one is p_cursor of type SYS_REFCURSOR (OUT param) and the other one is p_rank of type INT(IN param). But it showing an error.
DELIMITER $$
CREATE PROCEDURE sp_student(p_cursor OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM student WHERE rank = p_rank;
END$$
DELIMITER ;
the error what I'm getting is,
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 'OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM st' at line 1
I think I'm syntactically wrong for SYS_REFCURSOR.. please check my code and let me realise my mistake.
thanks in advance
mysql doesnt have refcursor like oracle, if u r planning to write a stored procedure that returns multiple rows/result set in mysql just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
call sample();
this will return a result set. which u can use.

Error Code: 1305 MySQL, Function does not Exist

I have a problem. I created a function in MySQL which returns a String (varchar data type).
Here's the syntax:
DELIMITER $$
USE `inv_sbmanis`$$
DROP FUNCTION IF EXISTS `SafetyStockChecker`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `SafetyStockChecker`
(jumlah INT, safetystock INT)
RETURNS VARCHAR(10) CHARSET latin1
BEGIN
DECLARE statbarang VARCHAR(10);
IF jumlah > safetystock THEN SET statbarang = "Stabil";
ELSEIF jumlah = safetystock THEN SET statbarang = "Perhatian";
ELSE SET statbarang = "Kritis";
END IF;
RETURN (statbarang);
END$$
DELIMITER ;
When I call the function like call SafetyStockChecker(16,16), I get this error:
Query : call SafetyStockChecker(16,16)
Error Code : 1305
PROCEDURE inv_sbmanis.SafetyStockChecker does not exist
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
What's wrong with the function?
That is not the correct way to call a function. Here's an example to call a function:
SELECT SafetyStockChecker(16,16) FROM TableName
The way you are doing now is for calling a STORED PROCEDURE. That is why the error says:
PROCEDURE inv_sbmanis.SafetyStockChecker does not exist
because it is searching for a Stored procedure and not a function.
You should use
SELECT SafetyStockChecker(16,16)

Postgres 9.0.4 : Error calling function which returns a ROWTYPE from within another function

I am experiencing unexpected behaviour on Postgres 9.0.4 using pl/pgsql relating to selecting from a function that returns a ROWTYPE into a ROWTYPE variable from within another function.
In the example below I:
Create a table, TESTTABLE and insert a row.
Create a function FN_TEST_GET_ROW that returns a row of ROWTYPE TESTTABLE based on selection of a single row from TESTTABLE
Create a test harness in the form of a function TESTX that calls FN_TEST_GET_ROW with ID=1
Call the test harness
The error shown below is returned unexpectedly
ERROR: invalid input syntax for integer: "(1,Fred)"
I would just expect the values (1, Fred) to be returned which is what happens if I execute
SELECT fn_test_get_row(1);
directly.
Create table:
CREATE TABLE testtable
(
id INTEGER,
name VARCHAR(10)
);
Add Data:
INSERT INTO testtable (id, name) VALUES (1, 'Fred');
Create function:
CREATE OR REPLACE FUNCTION fn_test_get_row(a INTEGER)
RETURNS testtable AS $$
DECLARE
i_row testtable;
BEGIN
SELECT *
INTO i_row
FROM testtable
WHERE id = a;
-- Success
RETURN i_row;
END;
$$ LANGUAGE plpgsql;
Create test function:
CREATE OR REPLACE FUNCTION testx()
RETURNS testtable AS $$
DECLARE
i_row testtable;
BEGIN
SELECT fn_test_get_row(1)
INTO i_row;
-- Success
RETURN i_row;
END;
$$ LANGUAGE plpgsql;
Execute the test function:
select testx();
Error returned:
ERROR: invalid input syntax for integer: "(1,Fred)"
CONTEXT: PL/pgSQL function "testx" line 8 at SQL statement
********** Error **********
ERROR: invalid input syntax for integer: "(1,Fred)"
SQL state: 22P02
Context: PL/pgSQL function "testx" line 8 at SQL statement
I've not seen the RETURNS tablename syntax before. I'd personally use RETURNS RECORD or RETURNS SETOF. Here are the fixed functions for you. What I did was change the testx function to treat fn_test_get_row() as a table, and change the result type of fn_test_get_row() to a set.
CREATE OR REPLACE FUNCTION fn_test_get_row(a INTEGER)
RETURNS SETOF testtable AS $$
DECLARE
i_row testtable%ROWTYPE;
BEGIN
SELECT INTO i_row * FROM testtable WHERE id = a;
RETURN NEXT i_row;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION testx()
RETURNS SETOF testtable AS $$
DECLARE
i_row testtable%ROWTYPE;
BEGIN
SELECT INTO i_row * FROM fn_test_get_row(1) AS foo;
RETURN NEXT i_row;
END;
$$ LANGUAGE plpgsql;
Which gives:
# select testx();
testx
----------
(1,Fred)
(1 row)
What also seems to work is to change the select in testx to the following:
SELECT (fn_test_get_row(1)).*
INTO i_row;
The error message makes sense if you consider that with out the parenthesis and star you are selecting one column of your record type. Postgres then tries to convert this column to the first column type of your result, leading to the error message you gave.