Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

mysql function not working correctly

Posted on 2011-09-02
4
Medium Priority
?
479 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 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

885 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