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 ?










share|improve this question




























    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 ?










    share|improve this question


























      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 ?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 12:39

























      asked Nov 13 at 12:01









      Chris

      629




      629
























          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.






          share|improve this answer





















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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.






          share|improve this answer





















          • 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















          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.






          share|improve this answer





















          • 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













          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?