Solved

mysql function not working correctly

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

21 Experts available now in Live!

Get 1:1 Help Now