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!
Related
I make a this stored function to return the amount of invoices per customer:
delimiter //
CREATE FUNCTION function1(id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = #id);
RETURN #result;
END//
delimiter ;
but when I use it, returns 0:
SELECT function1(12) AS Q;
and the query returns 428 :
SELECT count(invoice_id) AS Q FROM invoices WHERE customer_id = 12;
I need to know what am I doing wrong.
#id is not the same as id
But it id better to use variable names _id to differentiate variables from column names
delimiter //
CREATE FUNCTION function1(_id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = _id);
RETURN #result;
END//
delimiter ;
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?
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
I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)
I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;