使用coldfusion在我的查询中插入LIKE并在我的参数中使用通配符时遇到一些麻烦。以下是目前的工作原理:
<cfquery name="sample" datasource="database"><!---Take the query here--->
SELECT * <!---select all--->
FROM table <!---from the table "table"
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(selectList2,i)#" /> <!---
search column name = query parameter
using the same index in both lists
(selectList1) (selectList2) --->
<cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but
the very last element of the list (in that
case we don't need an "AND"--->
</cfloop>
</cfquery>
我的目标是将代码放到我可以输入类似参数的地方(但它当前不工作 - 错误读取无法执行查询)
<cfquery name="sample" datasource="database"><!---Take the query here--->
SELECT * <!---select all--->
FROM table <!---from the table "table"
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" /> <!---
search column name = query parameter
using the same index in both lists
(selectList1) (selectList2) --->
<cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but
the very last element of the list (in that
case we don't need an "AND"--->
</cfloop>
</cfquery>
答案 0 :(得分:0)
你的语法看起来很好。我在针对Microsoft SQL Server运行的一些代码中使用了相同类型的逻辑。
尝试此操作来调试代码并查看正在生成的SQL。
<!--- Comment out your query tag to debug <cfquery name="sample" datasource="database"> --->
<cfoutput> <!--- add a cfoutput tag to "see" your generated code --->
SELECT *
FROM table
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" />
<cfif i neq listLen(selectList1)>AND</cfif>
</cfloop>
</cfoutput> <!--- add a cfoutput tag to "see" your generated code --->
<!--- Comment out your query tag to debug </cfquery> --->
这很可能会破坏依赖于查询的其余代码。我通常只是在结束<cfabort>
标记之后粘贴</cfoutput>
标记以停止处理并向我显示SQL。
这样做的目的是将生成的SQL代码输出到浏览器。仔细看看,看看你是否能发现错误。您还可以将生成的SQL复制并粘贴到查询工具(查询分析器)中并以此方式进行测试。