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 ;
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.
I need to convert this tSQL query into MySQL query version 5.7.14
I tried declare parameter with the following syntax
declare #commissionType INT;
but I reached a dead end and need help to convert this query
CREATE PROCEDURE MyProc
#chainId int
as
BEGIN
declare #commissionMethod int
declare #commissionType int
declare #value decimal(18,2)
set #commissionMethod = 1
set #value = 200000
set #commissionType = (select CommissionType from CommissionRules
where ChainId = #chainId )
IF(#commissionMethod = 1)
BEGIN
print('alert')
select (Fixed * #value) / 100 from CommissionRules where ChainId = #chainId
END
END
This is pretty much a literal conversion. But check your logic. It doesn't make any sense. v_commissionMethod is always 1 and v_commissionType is not even being used after selecting a value.
DELIMITER //
CREATE PROCEDURE MyProc (
p_chainId int)
BEGIN
declare v_commissionMethod int;
declare v_commissionType int;
declare v_value decimal(18,2);
set v_commissionMethod = 1;
set v_value = 200000;
select CommissionType into v_commissionType from CommissionRules
where ChainId = p_chainId ;
IF(v_commissionMethod = 1)
THEN
/* print('alert') */
select (Fixed * v_value) / 100 from CommissionRules where ChainId = p_chainId;
END IF;
END;
//
DELIMITER ;
Something like this:
DELIMITER $$
CREATE PROCEDURE MyProc (
in_chainId unsigned
) as
BEGIN
select v_CommissionType := CommissionType,
v_commissionMethod = 1,
v_value = 200000
from CommissionRules
where ChainId = in_chainId;
if v_commissionMethod = 1 then
select 'alert';
select (Fixed * v_value) / 100
from CommissionRules
where ChainId = in_chainId
end if;
END;$$
DELIMITER ;
In MySQL I have added this function to calculate slope:
DELIMITER $$
CREATE FUNCTION regr_slope(x float, y float)
RETURNS float
DETERMINISTIC
BEGIN
DECLARE n int;
DECLARE sum_x float;
DECLARE sum_y float;
DECLARE sum_xx float;
DECLARE sum_xy float;
DECLARE slope float;
SET n = COUNT(x);
SET sum_x = SUM(x);
SET sum_y = SUM(y);
SET sum_xx = SUM(x*x);
SET sum_xy = SUM(x*y);
SET slope = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - POWER(sum_x, 2));
RETURN slope;
END$$
DELIMITER ;
However, when I use it like this:
SELECT regr_slope(pd.avg_velocity, pd.load_kg) FROM push_data
I get the error Invalid use of group function.
What could be the reason for this?
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;
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