2012-09-27 48 views
0

我使用cfscript語法創建查詢,並且我有兩個查詢參數是日期。我創建日期字符串使用使用cfscript在查詢對象addParam中投射日期的問題

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>語法創建查詢。

回答

6

value="createODBCDate(now())"

你忘了周圍的功能#跡象。沒有這些,它只是一個字符串。所以該函數從未被調用,並且最終傳入字符「createODBCDate(now())」作爲日期value

更新:

順便說一句,cf_sql_date自動刪除任何時間部分。所以雖然使用createODBCDate不會傷害任何東西,但它是多餘的。你可以簡單地寫:

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

所以我想這是一個什麼時候在cfscript塊中使用'#'的問題。正如我所說,這是我第一次使用cfscript創建查詢。 –

+0

不,它並不真正與'cfscript'相關。無論何時使用引號內的CF變量(即必須進行評估),您都需要#符號。請注意,您在'cfqueryparam'示例中使用了#號,因爲該函數嵌入在雙引號內。 – Leigh

+0

我現在看到了。我只是覺得他們不需要在一個cfscript標籤內出於某種原因。謝謝。 –

0

你的第二次嘗試需要#就像@Leigh提到的,也沒有引用你創建的變量「currentDate」。

+1

是的,儘管在這種情況下,您可以跳過額外的變量。事實上,你可以使用'value =「#now()#」',因爲'cf_sql_date'會自動刪除時間。所以'createODBCDate'是多餘的。 – Leigh