• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

MySQL Function with WHILE loop not working

I am trying to create a function that will accept two dates and some other criteria. Then I need to increment the date "dfrom" then test if a condition is true if so then it should return the date that the condition becomes true.

If the condition is not true then the date which is returned is the default.

Here is the function:
CREATE function stop_hit_date_d ( dfrom date, dto date, dsym varchar(10), dval DECIMAL(10,4) ) returns DATE
READS SQL DATA
begin
declare d DATE;
declare x INT;
declare f INT;
declare n INT;
set d = '1111-11-11';
set x = 1;
set f = 0;
set n = DATEDIFF(dto,dfrom);
while f = 0 OR n > 0 DO
    select 1 into f
    from prices
    where fk_tdate = ADDDATE(dfrom, INTERVAL x DAY)
        and fk_curr = 'USD'
        and fk_tfid = 1
        and fk_sym = dsym
        and low < dval
    limit 1;
    set x = x + 1;
    set d = fk_tdate;
    set n = n - 1;
end while;
return d;
end

When I run this query (I get the following error):
mysql> SELECT stop_hit_date_d ( '2011-08-23', '2011-09-09', 'AEP', 36.76);
ERROR 1054 (42S22): Unknown column 'fk_tdate' in 'field list'

In the above query I want to increment '2011-08-23' by 1 day, then see if the low price in the 'prices' table for symbol 'AEP' is below 36.76 if so then the function returns that incremented data, but if not the while loop adds another day to the dfrom date and re-test, if the dto date is hit and the condition is never true then the date which is returned is '1111-11-11'

Any help is apprecaited.
0
John_2357
Asked:
John_2357
  • 4
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
Hi.

It appears -- without looking deeper at the code -- your error is in the assignment of variable d.
CREATE function stop_hit_date_d ( dfrom date, dto date, dsym varchar(10), dval DECIMAL(10,4) ) returns DATE
READS SQL DATA
begin
declare d DATE;
declare x INT;
declare f INT;
declare n INT;
set d = '1111-11-11';
set x = 1;
set f = 0;
set n = DATEDIFF(dto,dfrom);
while f = 0 OR n > 0 DO
    select 1, fk_tdate into f, d 
    from prices 
    where fk_tdate = ADDDATE(dfrom, INTERVAL x DAY)
        and fk_curr = 'USD'
        and fk_tfid = 1
        and fk_sym = dsym
        and low < dval
    limit 1;
    set x = x + 1;
    set n = n - 1;
end while;
return d;
end

Open in new window


If you are only interested in fk_tdate, would it not also work to simply do this:
select min(fk_tdate) into d 
    from prices 
    where fk_tdate between dfrom and dto
        and fk_curr = 'USD'
        and fk_tfid = 1
        and fk_sym = dsym
        and low < dval;

Open in new window

0
 
John_2357Author Commented:
mwvisa1:

Thank you for the reply, either one works. I do have a couple questions, if you have the time.

1) select 1, fk_tdate into f, d  -- am I correct in assuming that "into" is assigning the string on the left into the variables on the right?

2) Am I correct in assuming that using your 2nd variation (no while loop) would be faster?

3) Is there a way I can write a while loop in mysql and have mysql print out the values of each variable as it loops through the while loop? Something similiar to:
while $i>0 do
i = i - 1
print i
end while;

Again thank you for your speedy reply and help.

john
0
 
Kevin CrossChief Technology OfficerCommented:
You are very welcome!

1) Yes, it assigns whatever values -- literal or from column -- to variables on the right based on the order, i.e., variables need to be listed in same order as column values.

2) That is the theory. It is a set based approached versus row-by-row analysis. The first is acting like a cursor.

3) I do not believe so. You can emulate it, by concatenating the values then doing a SELECT after the loop is done. No PRINT like T-SQL, though. At least none when I last checked.

Kevin
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
John_2357Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for John_2357's comment http:/Q_27306818.html#36535471

for the following reason:

Thank you
0
 
John_2357Author Commented:
I did not close or accept the solution until right now 18:15PST - I want to award the 500 points to "mwvisa1:" his solution was correct. I also do not want to make my question and its solution available to the public.

Thank you for fixing this.

John
0
 
John_2357Author Commented:
I did not close or accept the solution until right now 18:15PST - I want to award the 500 points to "mwvisa1:" his solution was correct. I also do not want to make my question and its solution available to the public.

Thank you for fixing this.

John
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now