Coldfusion:执行包含cfqueryparam的动态查询

Bin*_*nod 1 coldfusion refactoring cfquery cfqueryparam

一,代码.我正在制作一个查询结构:

<cfset myQueryStruct = { 
qone = "select * from t1 where column = <cfqueryparam cfsqltype='cf_sql_varchar' value='#arguments.a1#'>",
qtwo = "select * from t2 where column = <cfqueryparam cfsqltype='cf_sql_varchar' value='#arguments.a1#'>"
}>
Run Code Online (Sandbox Code Playgroud)

我需要动态执行查询,如下所示:

<cfquery name="qName" datasource="#dsn#">
    #myQueryStruct[arguments.type]#
</cfquery>
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near '<'. 
Run Code Online (Sandbox Code Playgroud)

似乎错误与<cfqueryparam cfsqltype='cf_sql_varchar' value='#arguments.a1#'>查询中的" "("<")有关

我尝试使用evaluate()并收到此错误:

Invalid CFML construct found on line 1 at column 15.
ColdFusion was looking at the following text:

t1
Run Code Online (Sandbox Code Playgroud)

我知道我们可以通过<cfif>在里面使用梯子来做到这一点,<cfquery>我的问题是,我们可以像我提到的那样(我必须遗漏某些东西)这样做,还是在CFML中不可能?

And*_*aus 6

问题是,您传递的字符串必须由ColdFusion处理.但你来不及了.CF已经解析了模板,因此字符串保持未翻译状态,因此会引发SQL错误.

我不确定,你想在这里完成什么,也许像这样的代码可以工作:

<cfscript>
dsn = "myDatabase";
criteria = "something";
myQueryStruct = {
    qone = "select * from t1 where column = ( :a1 ) ",
    qtwo = "select * from t2 where column = ( :a1 ) "
};

result1 = getQueryResult( queryString = myQueryStruct[ "qone" ], filterValue = criteria, dsn = dsn );
result2 = getQueryResult( queryString = myQueryStruct[ "qtwo" ], filterValue = criteria, dsn = dsn );

public function getQueryResult( queryString, filterValue, dsn ){
    var qry = new Query();
    var qryString = arguments.queryString;
    var queryResult = "";
    qry.setDatasource( arguments.dsn );
    qry.setSQl( qryString );
    qry.addParam( name="a1", value= arguments.filterValue, cfsqltype="cf_sql_varchar" ); 
    queryResult = qry.execute().getResult();
    return queryResult;
}
</cfscript>
Run Code Online (Sandbox Code Playgroud)