Solved

mysql function not working correctly

Posted on 2011-09-02
4
456 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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