Wrapper cffunction for query
Hello,
I am trying to write a wrapper function to be able to work with queries in cfscript (I deal with CF MX7). The SQL string can be assembled and passed to the function as an argument. Here is the function:
<cffunction name="QUERY" access="public" returntype="query">
<cfargument name="SQLString" type="string" required="yes">
<cfargument name="Datasource" type="string" required="no" default="#this.dsn#">
<cfargument name="dbType" type="string" required="no" default="">
<CFQUERY NAME="QryToReturn" Datasource="#arguments.Datasource#" dbtype="#arguments.dbType#">
#preserveSingleQuotes(arguments.SQLString)#
</CFQUERY>
<cfreturn QryToReturn>
</cffunction>
When it's as simple as this it works:
<cfscript>
Variables.iMyValue = 10;
sSQL = "SELECT * FROM t_test WHERE field1 = #Variables.iMyValue#";
Variables.qry1 = QUERY(SQLString: sSQL, DATASOURCE: Application.sDataSrc, DBTYPE: Application.sDBType);
</cfscript>
However I want to add cfqueryparam stuff to the queries and here is where I get errors.
I tried two ways to assemple the SQL:
<cfscript>
Variables.iMyValue = 10;
//this way the function gets value 10
sSQL1 = "SELECT * FROM t_test WHERE field1 = < cfqueryparam value=#Variables.iMyValue# cfsqltype='cf_sql_integer'>";
//this way the function gets a variable
sSQL2 = "SELECT * FROM t_test WHERE field1 = < cfqueryparam value=##Variables.iMyValue## cfsqltype='cf_sql_integer'>";
Variables.qry1 = QUERY(SQLString: sSQL1, DATASOURCE: Application.sDataSrc, DBTYPE: Application.sDBType);
Variables.qry2 = QUERY(SQLString: sSQL2, DATASOURCE: Application.sDataSrc, DBTYPE: Application.sDBType);
</cfscript>
Either way results in the same server error:
Error Executing Database Query.
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Oracle][ODBC][Ora]ORA-00936: missing expression
Could anybody tell me what I am doing wrong?
Thanks,
Alex
