views:

61

answers:

1

I suspect the answer to this is "No", based purely on the concept of type safety in general, but I shall ask anyway.

If I define a stored function in MySQL, normally part of the CREATE syntax defines the return type of function.

Is it possible to define one where the return type is one of two possible types, depending on the result, but which is unknown to the caller? Specifically, I'm interested in specifying DECIMAL types with two different precisions based on the value returned, so that I don't have to CAST these values explicitly everywhere they are used.

I know that I can explicitly use, for example, CASE and CAST to change the precision in a stored procedure or regular statement, but I'd like to abstract that code away into a stored function.

+1  A: 

My answer is "WHY?", not "No!". :-)

If it's not possible, create two stored procedures! One which will give the result with the highest precision and a second one that accepts parameters with lower precision which calls the high-precision function and it will cast the result back to the lower precision.

Two different functions with two different footprints, but basically they just do the same.

Abstraction is only useful when you're using an object-oriented approach. But stored procedures in databases aren't OO, just plain methods.

Workshop Alex
Why, meta-answering. I needed this information to improve an answer I provided elswhere. :)
Adam Bellaire