CFLib.org – Common Function Library Project

limit(inQry, arg1, arg2)

Last updated February 13, 2009

author

Andy Jarrett

Version: 2 | Requires: CF6 | Library: DatabaseLib

Description:
To be used on a recordset like a QoQ. Mimics MySQL's Limit function i.e. SELECT * FROM myTable LIMIT 0, 10 The above code will display the first 10 results from your table SELECT * FROM myTable LIMIT 5, 5 Starting from the 5th record this will bring back rows 5, 6, 7, 8, and 9. This functions aims to mimic this.

Return Values:
Returns a query.

Example:

<cfset qry = yourQuery />
<!--- Return rows 5-10 --->
<cfset newQuery = limit(qry, 5,5) />

Parameters:

Name Description Required
inQry Query to modify. Yes
arg1 Row to begin the limit. Yes
arg2 Number of rows to limit the result to. Yes

Full UDF Source:

<!---
 Mimics MySQL limit's function.
 v2 mods by Raymond Camden and Steven Van Gemert
 
 @param inQry      Query to modify. (Required)
 @param arg1      Row to begin the limit. (Required)
 @param arg2      Number of rows to limit the result to. (Required)
 @return Returns a query. 
 @author Andy Jarrett (mail@andyjarrett.co.uk) 
 @version 2, February 13, 2009 
--->
<cffunction name="limit" returntype="query" description="WORKS LIKE MYSQL LIMIT(N,N)" output="false">
    <cfargument name="inQry" type="query" hint="I am the query" />
    <cfargument name="arg1" type="numeric" />
    <cfargument name="arg2" type="numeric" />
    
    <cfscript>
    var outQry = arguments.inQry;
    var a1 = arguments.arg1-1;
    
    if(arg1 GT 1){
        outQry.RemoveRows(JavaCast( "int", 0 ), JavaCast( "int", a1 ));
    }
    
    outQry.RemoveRows(JavaCast( "int", arg2 ),JavaCast( "int", outQry.recordcount-arg2));
    
    return outQry;
    </cfscript>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson