我收到了以下错误,但不知道原因:
执行数据库查询时出错。MacromediaSQLServerIncorrect语法靠近')‘。错误发生在第141行。
<cfquery datasource="mySource" name="getTravel">
select traveler, customernumber, destination, tripdate, purpose, empid, pm_reportid, company
from mySource.dbo.PM_Travel
where pm_reportid in
(
<cfloop from="1" to="#getRecentReports.recordcount#" index="i">
<cfif i lt getRecentReports.recordcount>
#getRecentReports.pm_reportid[i]#,
<cfelse>
#getRecentReports.pm_reportid[i]#
</cfif>
</cfloop>
)
order by customernumber
</cfquery>第141行是:
where pm_reportid in (<cfloop from="1" to="#getRecentReports.recordcount#" index="i"><cfif i lt getRecentReports.recordcount>#getRecentReports.pm_reportid[i]#, <cfelse>#getRecentReports.pm_reportid[i]#</cfif></cfloop>)这不是我写的,我是CF的新手。任何帮助都是非常感谢的。
发布于 2014-08-01 17:44:20
试试这个:
where pm_reportid in ( <cfqueryparam value="#valueList(getrecentReports.pm_report_id)#" list="true" cfSqlType="CF_SQL_NUMERIC" /> )您可能需要更改cfSqlType的值,以匹配“pm_report_id列”中包含的数据类型。如果是文本,请尝试使用CF_SQL_VARCHAR
为了解释正在发生的事情:
valueList()采用'queryName.columnName‘语法的查询列,并将其转换为以逗号分隔的列表。
cfqueryparam将查询参数化,这将在SQL方面提高性能,并在数据作为特定数据类型传递时提供一些安全性。
发布于 2014-08-01 19:01:07
(对意见采取后续行动)
正如我所提到的,cfloop代码可能正在生成无效的sql语句。或者是由于包含零(0)条记录的查询,这将产生一个空子句:
WHERE Col IN ( {nothing here} ) ..。或者其中一个id为null,它将创建一个带有额外逗号的无效子句,例如:
WHERE Col (11,22,33,{no value here},44)斯科特的回答展示了一种处理此问题的方法。另一个选项是使用联接而不是循环查询结果。只需将匹配列上的两个表连接起来,并使用适当的WHERE过滤器即可。确切的SQL取决于两个表之间的关系,但下面是总的思路。我不知道所涉及的列的确切名称或数据类型,因此根据需要进行修改。
<cfquery ....>
SELECT t.traveler, t.customernumber, t.destination, ...more columns ...
FROM PM_Travel t INNER JOIN pm_reports r
ON t.pm_reportid = r.pm_reportid
WHERE r.empID = <cfqueryparam value="#form.empid#" cfsqltype="cf_sql_integer">
AND r.weekID = <cfqueryparam value="#form.weekid#" cfsqltype="cf_sql_integer">
</cfquery>附带注意,我知道这是一个继承的应用程序,但是一定要对所有用户提供的值使用cfqueryparam。这既是为了增强性能,也是为了提供额外的层保护,以防止常见形式的sql注入,正如Scott所提到的。您可以在CF文档中找到一个用dbms映射cfsqltypes类型。
https://stackoverflow.com/questions/25085600
复制相似问题