– Common Function Library Project

QuerySlice(theQuery, StartRow, NumberOfRows[, ColumnList])

Last updated May 23, 2005


Kevin Bridges

Version: 2 | Requires: CF6 | Library: DataManipulationLib

Pass this function a query and tell it what row to start with and how many rows to return. Good beginning for previous/next functionality.

Return Values:
Returns a query.


<CFSET Foo = QueryNew("name,age,rank")>
    <CFSET QueryAddRow(Foo)>
    <CFSET QuerySetCell(Foo,"name","Random Name #X#")>
    <CFSET QuerySetCell(Foo,"age",RandRange(20,50))>
    <CFSET QuerySetCell(Foo,"rank",RandRange(1,10))>
<CFDUMP VAR="#Foo#">
<CFSET Shorter = QuerySlice(Duplicate(Foo), 2, 5)>
<CFDUMP VAR="#Shorter#">


Name Description Required
theQuery The query to work with. Yes
StartRow The row to start on. Yes
NumberOfRows The number of rows to return. Yes
ColumnList List of columns to return. Defaults to all the columns. No

Full UDF Source:

 Returns specific number of records starting with a specific row.
 Renamed by RCamden
 Version 2 with column name support by Christopher Bradford,
 @param theQuery      The query to work with. (Required)
 @param StartRow      The row to start on. (Required)
 @param NumberOfRows      The number of rows to return. (Required)
 @param ColumnList      List of columns to return. Defaults to all the columns. (Optional)
 @return Returns a query. 
 @author Kevin Bridges ( 
 @version 2, May 23, 2005 
<cffunction name="QuerySliceAndDice" returntype="query" output="false">
    <cfargument name="theQuery" type="query" required="true" />
    <cfargument name="StartRow" type="numeric" required="true" />
    <cfargument name="NumberOfRows" type="numeric" required="true" />
    <cfargument name="ColumnList" type="string" required="false" default="" />
        var FinalQuery = "";
        var EndRow = StartRow + NumberOfRows;
        var counter = 1;
        var x = "";
        var y = "";
        if (arguments.ColumnList IS "") {
            arguments.ColumnList = theQuery.ColumnList;
        FinalQuery = QueryNew(arguments.ColumnList);
        if(EndRow GT theQuery.recordcount) {
            EndRow = theQuery.recordcount+1;
        QueryAddRow(FinalQuery,EndRow - StartRow);
        for(x = 1; x LTE theQuery.recordcount; x = x + 1){
            if(x GTE StartRow AND x LT EndRow) {
                for(y = 1; y LTE ListLen(arguments.ColumnList); y = y + 1) {
                    QuerySetCell(FinalQuery, ListGetAt(arguments.ColumnList, y), theQuery[ListGetAt(arguments.ColumnList, y)][x],counter);
                counter = counter + 1;
        return FinalQuery;


Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson