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;
}
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