使用cfscript在查询对象addParam中转换日期的问题

时间:2012-09-27 15:34:20

标签: coldfusion coldfusion-9

我正在使用cfscript语法创建查询,我有两个日期查询参数。我第一次使用

创建了日期字符串
queryservice.addParam(
     name="last_update",
     value="createODBCDate(now())",
     cfsqltype="cf_sql_date");

我认为这类似于:

<cfqueryparam value="#createODBCDate(now())#" cfsqltype="cf_sql_date">

所以,当我运行查询时,我得到了:

The cause of this output exception was that: coldfusion.runtime.Cast$DateStringConversionException: The value createODBCDate(now()) cannot be converted to a date.

精细。所以我创建了一个变量

var currentDate = createODBCDate(now());

将其添加到

queryservice.addParam(
     name="last_update",
     value="createODBCDate(now())",
     cfsqltype="cf_sql_date");

得到了

The cause of this output exception was that: coldfusion.runtime.Cast$DateStringConversionException: The value currentDate cannot be converted to a date.

当我使用标准<cfquery ...语法创建查询时,它运行正常。

所以,我假设我做错了什么,但我不能为我的生活找出那是什么。

顺便说一句,这是我第一次尝试使用<cfscript>语法创建查询。

2 个答案:

答案 0 :(得分:6)

  

value="createODBCDate(now())"

你忘记了函数周围的#符号。没有这些,它只是一个字符串。因此,永远不会调用该函数,并最终将文字字符“createODBCDate(now())”作为日期value传递。

更新:

另外,cf_sql_date会自动删除任何时间部分。因此,虽然使用createODBCDate不会伤害任何东西,但它是多余的。你可以简单地写:

    queryservice.addParam(
         name="last_update",
         value="#now()#",
         cfsqltype="cf_sql_date");

答案 1 :(得分:0)

你的第二次尝试需要#就像提到的@Leigh一样,也没有引用你创建的变量“currentDate”。

相关问题