List:General Discussion« Previous MessageNext Message »
From:Shawn Green Date:April 18 2010 1:47pm
Subject:Re: When to use Stored Procedures
View as plain text  
Hi Johan,

Johan De Meersman wrote:
> as a totally off-topc question, wouldn't something along the lines of
> LIMIT COUNT(*)/2, 1 do that trick?
> On 4/15/10, Rhino <rhino1@stripped> wrote:
>> ...snip...
>> For example, suppose you had to determine the median grade for a test.
>> ...

That would require running the query twice as LIMIT only accepts numeric 
literals. For a large dataset, that would destroy the efficiency of the 
stored procedure. It's more efficient with MySQL to capture the value in 
a temporary table, count those temporary results, then create a LIMIT 
query using the prepared statement syntax (dynamic SQL) against the data 
in the temp table.
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 

The above process could very easily be encapsulated by a stored 
PROCEDURE (but not by a stored FUNCTION) so that you would not need to 
implement it in your client code. Unfortunately the stored functions are 
not allowed to use prepared statements, yet.
SQL prepared statements (PREPARE, EXECUTE, DEALLOCATE PREPARE) can be 
used in stored procedures, but not stored functions or triggers.

Hope that helps!
Shawn Green
MySQL Principle Technical Support Engineer
Oracle USA, Inc.
Office: Blountville, TN
When to use Stored ProceduresTompkins Neil30 Mar
  • Re: When to use Stored ProceduresShawn Green15 Apr
    • Re: When to use Stored ProceduresRhino15 Apr
      • Re: When to use Stored ProceduresJohan De Meersman17 Apr
        • Re: When to use Stored ProceduresShawn Green18 Apr