CFLib.org – Common Function Library Project

cfquery(dsn, col, sql)

Last updated September 21, 2004

author

Joe Nicora

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Allows for the use of a function for a database return rather than th CFQUERY tag.

Return Values:
Returns a query.

Example:

<cfscript>
    qry = cfquery("accessblogdev","id,title","SELECT TOP 100 id,title from tblblogentries");
</cfscript>

<cfdump var="#qry#">

Parameters:

Name Description Required
dsn Datasource. Must be registered in the ODBC Control Panel. Yes
col List of columns. Yes
sql Sql to use. Yes

Full UDF Source:

/**
 * Mocks the CFQUERY tag.
 * 
 * @param dsn      Datasource. Must be registered in the ODBC Control Panel. (Required)
 * @param col      List of columns. (Required)
 * @param sql      Sql to use. (Required)
 * @return Returns a query. 
 * @author Joe Nicora (joe@seemecreate.com) 
 * @version 1, September 21, 2004 
 */
function cfquery(dsn,col,sql) {
    var datasource = dsn;    
    var userName = "";
    var password = "";
    var adOpenStatic = 3;
    var adLockReadOnly=1;
    var adCmdTxt = 1;
    var adGetRowsRest = -1;
    var columns = listToArray(col); 
    var strSQL = sql;
    var objDataConn = CreateObject("COM", "ADODB.Connection");
    var objDataRst = "";
    var intRecordCount = "";
    var arrRst = "";
    var qry = queryNew(arrayToList(columns));
    var thisCol = "";
    var thisRow = "";

    objDataConn.Open(Datasource, userName, password, -1);
    objDataRst = CreateObject("COM", "ADODB.Recordset");
    objDataRst.open(strSQL, objDataConn, adOpenStatic, adLockReadOnly, adCmdTxt);  
    intRecordCount = objDataRst.RecordCount;
    arrRst = objDataRst.GetRows(adGetRowsRest);
    
    queryAddRow(qry,intRecordCount);
    for (thisCol=1; thisCol LTE arrayLen(columns); thisCol=thisCol+1) {
        for (thisRow=1; thisRow LTE arrayLen(arrRst[thisCol]); thisRow=thisRow+1) {
        querySetCell(qry,columns[thisCol],arrRst[thisCol][thisRow],thisRow);
    }
    }
    objDataRST.close();
    objDataConn.close();
    return qry;
}

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