CFLib.org – Common Function Library Project

limit(inQry, arg1, arg2)

Last updated February 13, 2009

Version: 2 | Requires: ColdFusion MX | Library: DatabaseLib

 
Rated 5 time(s). Average Rating: 4.8

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:

view plain print about
<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:

view plain print about
<!---
 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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson