Solved

mysql function not working correctly

Posted on 2011-09-02
4
435 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
  • 2
  • 2
4 Comments
 
LVL 59

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 59

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now