我已经花了2个多小时来尝试用Google搜索答案。
我对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和工作台的语法规则的信息。
任何人都可以帮忙吗?
答案 0 :(得分:1)
您需要在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命令提示符运行此命令,它应该可以工作。