CFLib.org – Common Function Library Project

cfquery(dsn, col, sql)

Last updated September 21, 2004

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 1 time(s). Average Rating: 4.0

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

Return Values:
Returns a query.

Example:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson