I want to create function in MySQL 5.6. This function will by multiply param by count players. But I don't know how I can do it.
I think like this, but it's not working as intended.
DROP FUNCTION IF EXISTS do_it;
DELIMITER $$
CREATE FUNCTION do_it (s INT) RETURNS INT DETERMINISTIC
BEGIN
DECLARE k INT;
SELECT COUNT(id_player) as allPlayers FROM players;
SET k= allPlayers * s;
RETURN k;
END$$
DELIMITER ;
SELECT do_it(2);
Rather do it like this way
DECLARE k INT;
declare multiplyvar int;
SELECT COUNT(id_player) into multiplyvar FROM players;
SET k = multiplyvar * s;
RETURN k;
Related
Thats return me 0. Why? i need som like this 1/8 + 1/7 + 1/6....1/m
Srry for my code, im learning sql.
delimiter //
drop function if exists divide;
create function divide(m int) returns decimal(30,3) deterministic
begin
declare m int;
declare n decimal(30,3) default 0;
declare x decimal(30,3) default 0;
while m>=1 do
set n=n+x;
select 1/m into x;
set m=m-1;
end while;
return n;
end;//
delimiter ;
select divide(8);
Thanks you all, the solution was that i declared two times m.
trying to get the total number of boxes ordered from a given type("mytype").
something seems to be wrong with my syntax
create function NumOrdersForBoxType (mytype varchar(20))
returns int
begin
DECLARE #numorders int
select #numOrders = count(*)
from BOXES as B
where B.Type = mytype
return #numOrders
end
;
In mysql you don't declare user defined variables (at variables) stackoverflow.com/questions/11754781/… AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables
delimiter $$
create function f(mytype varchar(20))
returns int
begin
DECLARE numorders int;
set numorders = (select count(*)
from boxes as B
where B.Type = mytype
);
return numOrders;
end $$
delimiter ;
I have the following function:
DELIMITER $$
create function fp_v2.fp_splitadjprice (id char(8), startdate date)
returns float
begin
declare splitfactor float;
declare splitadjprice float;
declare spinofffactor float;
set splitfactor = 1.0;
select splitfactor = fp_v2.fp_splitfactor_prices(id, startdate);
select splitadjprice = convert(float,p_price * splitfactor)
from fp_v2.fp_basic_prices p
where fsym_id = id and p_date = startdate;
return splitadjprice;
END$$
DELIMITER ;
I get an error message at the 2nd select statement saying "expected a ( or with".
I really don't understand the syntax of MySQL workbench. It seems pretty random to me. Like when should I put ; and when should I not?. What are the rules?
You need use SELECT .. INTO to update your variables.
convert sintaxis is CONVERT(value, type) and you cant use FLOAT as type need use DECIMAL and you probably dont need it anyway because your both variable are float already.
DELIMITER $$
create function fp_v2.fp_splitadjprice (id char(8), startdate date)
returns float
begin
declare splitfactor float;
declare splitadjprice float;
declare spinofffactor float;
set splitfactor = 1.0;
SELECT fp_v2.fp_splitfactor_prices(id, startdate) INTO splitfactor ;
SELECT convert(p_price * splitfactor, DECIMAL (10,4) INTO splitadjprice
FROM fp_v2.fp_basic_prices p
WHERE fsym_id = id and p_date = startdate;
return splitadjprice;
END$$
DELIMITER ;
I have a mysql procedure that counts number of activated users in members table. I want the procedure to, when done counting, display result stored in variable num in table. But I can select num only inside the loop, so every iteration new table is displayed, and when I put select after loop, nothing is being displayed.
delimiter $$
create procedure users()
begin
declare un varchar(25);
declare num int default 0;
declare idd,act int;
declare c cursor for
select id,username,activated from members;
open c;
l: loop
fetch c into idd,un,act;
if act = 1 then set num = num+1;
end if;
end loop l;
close c;
select num; <---------------
end$$
delimiter ;
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