CFLib.org – Common Function Library Project

queryRemoveColumns(theQuery, columnsToRemove)

Last updated March 01, 2005

author

Giampaolo Bellavite

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Remove a list of columns from a specified query, using a query of query.

Return Values:
Returns a query.

Example:

<cfset aQuery=QueryNew('first_column,second_column,third_column,fourth_column')>
<cfset QueryAddRow(aQuery,1)>
<cfset QuerySetCell(aQuery,'first_column',1)>
<cfset QuerySetCell(aQuery,'second_column',2)>
<cfset QuerySetCell(aQuery,'third_column',3)>
<cfset QuerySetCell(aQuery,'fourth_column',4)>

<cfdump var="#aQuery#">    
<cfdump var="#queryRemoveColumns(aQuery,'second_column,third_column')#">

Parameters:

Name Description Required
theQuery The query to manipulate. Yes
columnsToRemove A list of columns to remove. Yes

Full UDF Source:

<!---
 Remove a list of columns from a specified query.
 
 @param theQuery      The query to manipulate. (Required)
 @param columnsToRemove      A list of columns to remove. (Required)
 @return Returns a query. 
 @author Giampaolo Bellavite (giampaolo@bellavite.com) 
 @version 1, April 14, 2005 
--->
<cffunction name="queryRemoveColumns" output="false" returntype="query">
    <cfargument name="theQuery" type="query" required="yes">
    <cfargument name="columnsToRemove" type="string" required="yes">
    <cfset var columnList=theQuery.columnList>
    <cfset var columnPosition="">
    <cfset var c="">
    <cfset var newQuery="">
    <cfloop list="#arguments.columnsToRemove#" index="c">
        <cfset columnPosition=ListFindNoCase(columnList,c)>
        <cfif columnPosition NEQ 0>
            <cfset columnList=ListDeleteAt(columnList,columnPosition)>
        </cfif>
    </cfloop>
    <cfquery name="newQuery" dbtype="query">
        SELECT #columnList# FROM theQuery
    </cfquery>
    <cfreturn newQuery>
</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