Getting syntax errors when creating function
up vote
0
down vote
favorite
I have been spending over 2 hours now trying to google my way to the answer.
I am completely new to SQL and MySQL and I have tried to write the following function:
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)
But I get multiple syntax errors. I have written the errors where I get them.
I have a hard time finding information about the syntax rules of MySQL and the workbench.
Can anyone help ?
mysql sql mysql-workbench
add a comment |
up vote
0
down vote
favorite
I have been spending over 2 hours now trying to google my way to the answer.
I am completely new to SQL and MySQL and I have tried to write the following function:
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)
But I get multiple syntax errors. I have written the errors where I get them.
I have a hard time finding information about the syntax rules of MySQL and the workbench.
Can anyone help ?
mysql sql mysql-workbench
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have been spending over 2 hours now trying to google my way to the answer.
I am completely new to SQL and MySQL and I have tried to write the following function:
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)
But I get multiple syntax errors. I have written the errors where I get them.
I have a hard time finding information about the syntax rules of MySQL and the workbench.
Can anyone help ?
mysql sql mysql-workbench
I have been spending over 2 hours now trying to google my way to the answer.
I am completely new to SQL and MySQL and I have tried to write the following function:
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)
But I get multiple syntax errors. I have written the errors where I get them.
I have a hard time finding information about the syntax rules of MySQL and the workbench.
Can anyone help ?
mysql sql mysql-workbench
mysql sql mysql-workbench
edited Nov 13 at 12:39
asked Nov 13 at 12:01
Chris
629
629
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need to provide delimiter in MySql workbench to tell where your code begins and ends.
Assuming your syntax is correct, you can write as below.
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 ;
You can also, run this from MySQL command prompt and it should work.
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to provide delimiter in MySql workbench to tell where your code begins and ends.
Assuming your syntax is correct, you can write as below.
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 ;
You can also, run this from MySQL command prompt and it should work.
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
add a comment |
up vote
1
down vote
accepted
You need to provide delimiter in MySql workbench to tell where your code begins and ends.
Assuming your syntax is correct, you can write as below.
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 ;
You can also, run this from MySQL command prompt and it should work.
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to provide delimiter in MySql workbench to tell where your code begins and ends.
Assuming your syntax is correct, you can write as below.
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 ;
You can also, run this from MySQL command prompt and it should work.
You need to provide delimiter in MySql workbench to tell where your code begins and ends.
Assuming your syntax is correct, you can write as below.
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 ;
You can also, run this from MySQL command prompt and it should work.
answered Nov 13 at 13:02
VikiT
436
436
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
add a comment |
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
Thanks man. Do you have 5 minutes for private chat? I have a few questions.
– Chris
Nov 13 at 13:09
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
@Chris, glad it worked. I do not use chat and only try to help, when get time. If you have any specific questions, you can post again and someone will take a look. Have a good day.
– VikiT
Nov 13 at 13:27
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280616%2fgetting-syntax-errors-when-creating-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown