回答标题中的问题:不,这不是 CFScript 中的错误。您所展示的内容与 CFScript 完全无关。
但是,要回答您要问的问题:是的,您在 Query.cfc 中发现了一个错误。
这是一些演示该错误的代码(并证明它与 CFScript 无关,而与 Query.cfc 无关)。
此代码工作正常:
<cfset query = new Query(datasource="scratch_mssql")>
<cfset query.setSql("
SELECT '''' + email + '''' as email
FROM users
WHERE firstName = '#firstName#'
")>
<cfset emailAddresses = query.execute().getResult()>
<cfdump var="#emailAddresses#">
请注意,我已将过滤器值硬编码到 SQL 字符串中。呸。
此代码错误:
<cfset query = new Query(datasource="scratch_mssql")>
<cfset query.setSql("
SELECT '''' + email + '''' as email
FROM users
WHERE firstName = :firstname
")>
<cfset query.addParam(name="firstname", value=firstName, cfsqltype="CF_SQL_VARCHAR")>
<cfset emailAddresses = query.execute().getResult()>
<cfdump var="#emailAddresses#">
我的错误是:
[Macromedia][SQLServer JDBC Driver][SQLServer]An object or column name is missing or empty.
For SELECT INTO statements, verify each column has a name. For other statements,
look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
仅供参考:CF 传递给数据库的 SQL 是:
SELECT '' '' + email + '' '' as email FROM users WHERE firstName = (param 1)
为了证明它是由 ColdFusion 错误处理您的单引号引起的,这有效:
<cfset query = new Query(datasource="scratch_mssql")>
<cfset query.setSql("
SELECT email as email
FROM users
WHERE firstName = :firstname
")>
<cfset query.addParam(name="firstname", value=firstName, cfsqltype="CF_SQL_VARCHAR")>
<cfset emailAddresses = query.execute().getResult()>
<cfdump var="#emailAddresses#">
所以如果我是你,我会为此提出一个错误。注:CF9.0.2 和 CF10.0.7 相同
但是,同样......如果我是你,无论如何我都不会把那些引号放在那里。除非有很好的理由,否则请在显示时将它们插入,而不是在进行数据处理时插入。我想它们是为了展示目的吗?