创建函数时出现语法错误

i2byvkas  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(342)

我已经花了两个多小时的时间来搜索我的答案。
我对sql和mysql完全陌生,我尝试编写以下函数:

CREATE FUNCTION fp_spinofffactor (id char(8), startdate date)
RETURNS float
BEGIN
DECLARE spinoffFactor float;               (ERROR- EXPECTED A ";")
select spinoffFactor = ISNULL(EXP(SUM(LOG(spinoffFactor))),1)
from( 
select case when (prev_price- divs) <= 0 THEN 1 
else (prev_price- divs)/prev_price end as spinoffFactor
    from (select
    divs,
    fp_v2.fp_prevUnadjPrice(id, ex_date) as prev_price
from (
select sum(fbd.p_divs_pd) as divs,fbd.p_divs_exdate as ex_date
from fp_v2.fp_basic_dividends fbd
where fbd.fsym_id = id
and fbd.p_divs_s_pd=1
and fbd.p_divs_exdate > startdate
group by fbd.p_divs_exdate ) a ) b ) c; 
return spinofffactor;              ERROR (Return is not valid at this position)
END                                ERROR (END IS NOT VALID AT THIS position)

但是我有很多语法错误。我已经把错误写在哪里了。
我很难找到有关mysql和工作台语法规则的信息。
有人能帮忙吗?

yb3bgrhw

yb3bgrhw1#

您需要在mysql工作台中提供分隔符来告诉代码的开始和结束位置。假设你的语法是正确的,你可以写如下。

DELIMITER $$
CREATE FUNCTION fp_spinofffactor (id char(8), startdate date)
RETURNS float
BEGIN
DECLARE spinoffFactor float;       
    select spinoffFactor = ISNULL(EXP(SUM(LOG(spinoffFactor))),1)
    from( 
    select case when (prev_price- divs) <= 0 THEN 1 
    else (prev_price- divs)/prev_price end as spinoffFactor
        from (select
        divs,
        fp_v2.fp_prevUnadjPrice(id, ex_date) as prev_price
    from (
    select sum(fbd.p_divs_pd) as divs,fbd.p_divs_exdate as ex_date
    from fp_v2.fp_basic_dividends fbd
    where fbd.fsym_id = id
    and fbd.p_divs_s_pd=1
    and fbd.p_divs_exdate > startdate
    group by fbd.p_divs_exdate ) a ) b ) c; 
return spinofffactor;              
END$$
DELIMITER ;

您也可以从mysql命令提示符运行它,它应该可以工作。

相关问题