Create mysql function failed in phpmyadmin - mysql

When I try to make this function it always failed.
DELIMITER $$
CREATE FUNCTION `f_hadir`(ID INT)
RETURNS INT
BEGIN
DECLARE TMP INT;
SELECT COUNT(ID_presensi_siswa) into TMP
FROM presensi_siswa
WHERE keterangan='Hadir' AND id_siswa=id LIMIT 1;
RETURN TMP;
END$$
DELIMITER ;
i had similiar code but and it successfully created.
DROP FUNCTION IF EXISTS `CategoriesToString`;
DELIMITER ;;
CREATE FUNCTION `CategoriesToString`(TID int) RETURNS varchar(500) CHARSET utf8
BEGIN
DECLARE result varchar(500);
SELECT group_concat(Category.Name) INTO result FROM Category
WHERE ID IN (SELECT CategoryID FROM TopicHasCategory WHERE TopicID = TID) LIMIT 1;
RETURN result;
END;;
DELIMITER ;
Anyone have idea?

Related

MySql stored functions: Not works

I have the following function:
DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(adapter_id int(10), view_id int(10),name varchar(255)) RETURNS TINYINT(1)
BEGIN
DECLARE retOK TINYINT DEFAULT 0;
IF (SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) = 0 THEN
INSERT INTO `tables` (`adapter_id`,`view_id`,`name`) VALUES (adapter_id, view_id, name);
SET retOK = 1;
END IF;
RETURN retOK;
END;
$$
DELIMITER ;
When i call the function to insert a new row with
SELECT saveTableRow(3,1,'Text');
I get the result '0' and there is no new row saved.
It might be a name collision problem. The name of the column is the same with the name of you parameter. You need to change the name of your parameter that is different from the name of your column. eg,
DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(
_adapter_id int(10),
_view_id int(10),
_name varchar(255))
RETURNS TINYINT(1)
BEGIN
DECLARE retOK TINYINT DEFAULT 0;
IF (SELECT COUNT(*)
FROM `tables`
WHERE `adapter_id`=_adapter_id AND
`view_id`=_view_id AND
`name`=_name ) = 0 THEN
INSERT INTO `tables` (`adapter_id`,`view_id`,`name`)
VALUES (_adapter_id, _view_id, _name);
SET retOK = 1;
END IF;
RETURN retOK;
END;
$$
DELIMITER ;
change the if as follows as try please:
IF ((SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) < 1)

How to return a resultset from StoredProcedure using MySql?

I am writing a stored procedure using MySql which returns multiple rows using select statement.
My code is as below
drop procedure if exists GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SET #resultset = (SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER
Any examples of how to return a resultSet in storedProcedure?
Thanks
Gendaful
DROP PROCEDURE IF EXISTS GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER ;

No return found in function

I have browsed around and other solutions don't seem to be working for me here. I keep getting a 'No return found for functon' error when trying to create this function in MySQL. Any idea why?
CREATE FUNCTION `mydb`.`myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
SELECT SUM(Transaction.Bought) INTO #Qty FROM Transaction WHERE Transaction.Name = Name;
RETURN #Qty
Try this
DELIMITER $$
CREATE FUNCTION `myfunction`(`Name` VARCHAR(20) CHARSET utf8) RETURNS INT NOT DETERMINISTIC
READS SQL DATA
MAIN: BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal FROM `Transaction` WHERE `Transaction`.Name = Name;
RETURN returnVal;
END MAIN;$$
DELIMITER ;
Alternatively, try this,
DELIMITER $$
CREATE FUNCTION `myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal
FROM `Transaction`
WHERE `Transaction`.Name = Name;
RETURN returnVal;
END$$
DELIMITER ;
delimiter //
create function myfun1234 (i int,j int)
returns int
begin
declare x int ;
declare y int ;
declare z int ;
set x=10;
set y=20;
if (1<=x && j>=y )then
set z=i+j;
end if;
return z;
end; //
-- error1 FUNCTION myfun12 ended without RETURN

Mysql Stored Procedure isn't there?

I have this Stored Procedure. When I execute it in my PhpMyAdmin on Wamp, it says it executed and everything. When I try to use it in a query, it says the function is not there. What am I doing wrong?
DELIMITER //
CREATE PROCEDURE `sql_level`(IN exp INT)
BEGIN
SELECT id
FROM `levels`
WHERE experience <= exp DESC LIMIT 1;
id = id-1;
END //
DELIMITER ;
Here is the Query I'm trying to run.
$id = 1;
if($stmt->prepare("SELECT sql_level(attack) FROM `users` WHERE id = ?")) {
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->bind_result($attack);
$stmt->fetch(); echo "<h1 align='center'>".$attack."</h1>"; }
else { die($stmt->error); }
Thanks.
You want to create your routine as a function instead of a stored procedure.
DELIMITER //
CREATE FUNCTION `sql_level`(exp INT)
RETURNS INT
BEGIN
DECLARE ReturnId INT;
SELECT id-1 INTO ReturnId
FROM `levels`
WHERE experience <= exp DESC LIMIT 1;
RETURN ReturnId;
END //
DELIMITER ;
DELIMITER |
CREATE FUNCTION `sql_level`(exp INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE ReturnId INT;
SELECT id-1 INTO ReturnId
FROM `levels`
WHERE experience <= exp;
RETURN ReturnId;
END;
This is what I ended up using to make it work. Thanks! Hope this helps someone!

What's wrong with this MySQL Stored Function?

Having trouble getting this to apply in MySQL Workbench 5.2.15
DELIMITER //
CREATE
DEFINER=`potts`#`%`
FUNCTION
`potts`.`fn_create_category_test` (test_arg VARCHAR(50))
RETURNS int
BEGIN
DECLARE new_id int;
SET new_id = 8;
RETURN new_id;
END//
The actual function will have a lot more between BEGIN and END but as it stands, even this 3 liner won't work.
Thanks!
DELIMITER $$
CREATE FUNCTION `fn_create_category_test` (test_arg varchar(50))
RETURNS INT
BEGIN
DECLARE new_id int;
set new_id=8;
return new_id;
END $$
DELIMITER ;
Works fine for me, try getting rid of DEFINER?