Solved

mysql function not working correctly

Posted on 2011-09-02
4
463 Views
Last Modified: 2012-05-12
The goal is to pass in a date, and then to keep incrementing this date until we find this date existing in a table.

I tired various queries, the first one is:

CREATE function data_exist_date ( dfrom date ) returns DATE
READS SQL DATA
begin
declare d DATE;
declare x INT;
declare f INT;
set d = '1111-11-11';
set x = 1;
set f = 0;

while f = 0 DO

        -- when there is a valid price for GE on this date make f=1, exiting the while loop
    select 1 into f 
        -- when there is a valid price for GE on this date make d = to the 1st date found after the passed in date
    select fk_tdate into d 

    from prices 
    where fk_tdate = ADDDATE(dfrom, INTERVAL x DAY)
        and fk_curr = 'USD'
        and fk_sym = 'GE'
    limit 1;

        -- INC the counter so we keep adding another day to the INTERVAL
    set x = x + 1;

end while;
        -- return the first date found with valid prices after the passed in date.
return d;
//

Open in new window


I got this error (I guess I can not use multiple "select xxx into yyyy' stmts):

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select fk_tdate into d

    from prices
    where fk_tdate = ADDDATE(dfrom, IN' at line 16


The second query is:

delimiter //
CREATE function data_exist_date ( dfrom date ) returns DATE
READS SQL DATA
begin
declare d DATE;
declare x INT;
declare f INT;
set d = '1111-11-11';
set x = 1;
set f = 0;

while f = 0 DO

        -- when there is a valid price for GE on this date make f=1, exiting the while loop
    select 1 into f 
    from prices 
    where fk_tdate = ADDDATE(dfrom, INTERVAL x DAY)
        and fk_curr = 'USD'
        and fk_sym = 'GE'
    limit 1;
        
        -- when there is a valid price for GE on this date make d = to the 1st date found after the passed in date
    select fk_tdate into d 
    from prices 
    where fk_tdate = ADDDATE(dfrom, INTERVAL x DAY)
        and fk_curr = 'USD'
        and fk_sym = 'GE'
    limit 1;

        -- INC the counter so we keep adding another day to the INTERVAL
    set x = x + 1;

end while;
        -- return the first date found with valid prices after the passed in date.
return d;
//

Open in new window


I get this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 33

So, what is the correct syntax? All I am trying to do is to accept a date then increment it one date, test a table to see if this incremented date exist and if not to increment again until we find the date which exist in the table, and then return that date.

Thank you in advance
0
Comment
Question by:John_2357
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36475468
John_2357,

First, I moved the code to snippet blocks for you, but have not yet read through it all the way. I will if needed, but I had a thought just after reading the question body of what you are wanting to do. Hopefully this helps and eliminates the need for loop.

If you are wanting the "date [passed], and then to keep incrementing this date until we find this date existing in a table." Then can probably just do a lookup on your table and get MIN() or first date using LIMIT 1 that is >= the passed date.

e.g.,

DECLARE d DATE;
SELECT MIN(fk_tdate) INTO d
FROM prices
WHERE fk_tdate >= dfrom
   AND fk_curr = 'USD'
   AND fk_sym = 'GE'
;

Let's start with that.
0
 
LVL 1

Author Comment

by:John_2357
ID: 36475844
Thank you for the advice, I was able to create following script and it worked perfect;

delimiter //
CREATE function data_exist_nxt ( dfrom date ) returns DATE
READS SQL DATA
begin
declare d DATE;
select MIN(fk_tdate) into d from prices
where fk_tdate > dfrom
and fk_curr = 'USD'
and fk_sym = 'GE'
limit 1;
return d;
end
//


However,  let me ask you 2 ?'s about my while loops.
1) "select xx into yy"  - am I limited to just using one of these?
2) Do you see any obvious errors in my while loops?

Thanks again.

John
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36475974
For the first one, the issue is the double SELECT. That will not work. If you were trying to SET both variables at the same time, you can use INTO var1, var2 in same order as data.

e.g.,
select 1, fk_tdate into f, d ... ;

or:
select col1, col2 from your_table into var1, var2;

For the latter, it may be the missing END.
0
 
LVL 1

Author Closing Comment

by:John_2357
ID: 36476256
thanks, have a good weekend!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question