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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson