![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
delimiter // CREATE FUNCTION Add_Working_Days(Units_to_Add Decimal(10, 2), Start_Date Date) RETURNS date BEGIN DECLARE whole_units int; SET whole_units = ROUND(Units_to_Add, 0); RETURN SELECT Max(T1.Reporting_Day) FROM ( SELECT * FROM reporting_days WHERE (Day_Type ='') AND (reporting_day >= Start_Date) Limit whole_units) as T1; |
#3
| |||
| |||
|
|
On 2010-12-16 06:46, MartinH wrote: [...] delimiter // CREATE FUNCTION *Add_Working_Days(Units_to_Add Decimal(10, 2), Start_Date Date) * * RETURNS date BEGIN * * DECLARE whole_units int; * *SET whole_units = ROUND(Units_to_Add, 0); * * RETURN SELECT Max(T1.Reporting_Day) FROM ( * * * * SELECT * FROM reporting_days WHERE (Day_Type ='') AND (reporting_day >= Start_Date) Limit whole_units) as T1; I think you will get the wrong result here. You return the latest day from n random days after start_date. You need to order the result set for this to work. /Lennart |
#4
| |||
| |||
|
|
I think you will get the wrong result here. You return the latest day from n random days after start_date. You need to order the result set for this to work. /Lennart Agreed, I will need to order the results from the subquery, but would this be causing the syntax error? |
#5
| |||
| |||
|
|
Here is the SQL I've tried: CREATE FUNCTION Add_Working_Days(Units_to_Add Decimal(10, 2), Start_Date Date) RETURNS date BEGIN DECLARE whole_units int; SET whole_units = ROUND(Units_to_Add, 0); RETURN SELECT Max(T1.Reporting_Day) FROM ( SELECT * FROM reporting_days WHERE (Day_Type ='') AND (reporting_day >= Start_Date) Limit whole_units) as T1; END I've also tried assigning the return value to a parameter and (using 'SET varReturn = SELECT . . .) and still get a syntax error with the SELECT part of the function. |
|
Can anybody point out where I'm going wrong, and also, if there is a better way to return a scalar value from a query? |
#6
| |||
| |||
|
|
MartinH <mgh... (AT) gmail (DOT) com> wrote: Here is the SQL I've tried: CREATE FUNCTION *Add_Working_Days(Units_to_Add Decimal(10, 2), Start_Date Date) * * RETURNS date BEGIN * * DECLARE whole_units int; * *SET whole_units = ROUND(Units_to_Add, 0); * * RETURN SELECT Max(T1.Reporting_Day) FROM ( * * * * SELECT * FROM reporting_days WHERE (Day_Type ='') AND (reporting_day >= Start_Date) Limit whole_units) as T1; END I've also tried assigning the return value to a parameter and (using 'SET varReturn = SELECT . . .) and still get a syntax error with the SELECT part of the function. ... Can anybody point out where I'm going wrong, and also, if there is a better way to return a scalar value from a query? The first way does not work because a function cannot return a result set. A result set (even with only one row) is not a scalar. Your second thought is much better: you have to put the result into a variable and then RETURN that. You just use the wrong syntax. http://dev.mysql.com/doc/refman/5.1/...statement.html XL |
#7
| |||
| |||
|
|
On Dec 15, 11:55 pm, Axel Schwenke<axel.schwe... (AT) gmx (DOT) de> wrote: MartinH<mgh... (AT) gmail (DOT) com> wrote: Here is the SQL I've tried: CREATE FUNCTION Add_Working_Days(Units_to_Add Decimal(10, 2), Start_Date Date) RETURNS date BEGIN DECLARE whole_units int; SET whole_units = ROUND(Units_to_Add, 0); RETURN SELECT Max(T1.Reporting_Day) FROM ( SELECT * FROM reporting_days WHERE (Day_Type ='') AND (reporting_day>= Start_Date) Limit whole_units) as T1; END I've also tried assigning the return value to a parameter and (using 'SET varReturn = SELECT . . .) and still get a syntax error with the SELECT part of the function. ... Can anybody point out where I'm going wrong, and also, if there is a better way to return a scalar value from a query? The first way does not work because a function cannot return a result set. A result set (even with only one row) is not a scalar. Your second thought is much better: you have to put the result into a variable and then RETURN that. You just use the wrong syntax. http://dev.mysql.com/doc/refman/5.1/...statement.html XL Thanks for the guidance - I've managed to narrow down the syntax error to the use of the whole_units parameter as the value for the LIMIT on the sub query results. When I hard code a numeric value it works, but not with the parameter. I have found in the mySQL documentation that: The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements). |
|
As I have already specified the whole_units parameter as an INT I'm not sure what to try next? |
#8
| |||
| |||
|
|
As I have already specified the whole_units parameter as an INT I'm not sure what to try next? Any suggestions would be appreciated. |
#9
| |||
| |||
|
|
On 2010-12-16 11:43, MartinH wrote: [...] As I have already specified the whole_units parameter as an INT I'm not sure what to try next? Any suggestions would be appreciated. One way is to use a cursor inside the function. Example: delimiter // CREATE FUNCTION *Add_Working_Days(Units_to_Add Decimal(10, 2), Start_Date Date) * * RETURNS date BEGIN * * DECLARE whole_units int; * * DECLARE res DATE; * * DECLARE cur1 CURSOR FOR SELECT * FROM reporting_days * * * * * * * * * * * * WHERE reporting_day >= Start_Date * * * * * * * * * * * * ORDER BY reporting_day; * * SET whole_units = ROUND(Units_to_Add, 0); * * OPEN cur1; * * WHILE whole_units >= 0 DO * * * * FETCH cur1 into res; * * * * SET whole_units = whole_units - 1; * * END WHILE; * * RETURN res; END// delimiter ; /Lennart |
#10
| |||
| |||
|
|
I have used your code which creates the function, but then when I try and use it like this: SELECT Add_Working_Days_Cursor(10.23, '2010-12-01') I get an "#1328 - Incorrect number of FETCH variables" error. Any suggestions? |
|
Add_Working_Days(1.23, '2010-01-01') | +--------------------------------------+ 2010-01-02 | +--------------------------------------+ |
![]() |
| Thread Tools | |
| Display Modes | |
| |