2017-07-13 148 views
0

我有一個查詢:從值列表中刪除空

<cfif topic NEQ ""> 
    <cfquery name="queryTopicName" datasource="#ODBC#"> 
     select topic as topicName from ltbTopics where topicId in (#topic#) 
    </cfquery> 
    <cfset selectedRiskCategories = ValueList(queryTopicName.topicName)> 
</cfif> 

這裏的「#話題#」包含其第一個值是空列表,所以它有喜歡,, 51,52,等所以它提供了一個錯誤:

「附近有語法錯誤‘’

線路33" 發生
的錯誤。任何一個可以幫助我在這個如何解決這個?

+0

你提到一個空。該列表是否來自另一個查詢?如果是,並且只使用該查詢來檢索此值,那麼您可以將其包含在其他選擇主題中作爲來自ltbTopics的topicName,其中topicId位於(從some_other_table選擇主題where something = something_else) –

回答

1

有很多方法可以做到這一點。但簡單的黑客就是將列表轉換爲數組,然後返回列表。

<cfif topic NEQ ""> 
<cfset arrayTopic = ListToArray(topic)> 
<cfset topic = ArrayToList(arrayTopic)> 
<!---you may need some more validations as it is possible that original list only has commas in it---> 
    <cfquery name="queryTopicName" datasource="#ODBC#"> 
     select topic as topicName from ltbTopics where topicId in (#topic#) 
    </cfquery> 
    <cfset selectedRiskCategories = ValueList(queryTopicName.topicName)> 
</cfif> 
+0

嗨,謝謝爲了發佈這個建議,能否詳細說明如何在上面的代碼中實現它? –

+0

非常感謝你的回答。 –

+0

在您的查詢中使用'cfqueryparam'來防止SQL注入,並避免執行計劃重新生成。 – Beginner

1

謝謝你們的答案,最終的查詢,對我 完美工作是:

<!--- Query to extract selected risk category filters ---> 
    <cfif topic NEQ ""> 
     <cfset arrayTopic = ListToArray(topic)> 
     <cfset topic = ArrayToList(arrayTopic)> 

     <cfquery name="queryTopicName" datasource="#ODBC#"> 
    select 
     topic as topicName 
    from 
     ltbTopics 
    where 
     topicId in 
     (
      <cfqueryparam 
      value = "#topic#" 
      cfsqltype= "CF_SQL_INTEGER" 
      list = "true" 
     />) 
     </cfquery> 
     <cfset selectedRiskCategories = ValueList(queryTopicName.topicName)> 
    </cfif> 

    Once again thanks for your help, I really appreciate it. 
+0

我推薦循環和測試整數或使用justNumericList UDF(首選)http://cflib.org/udf/justNumericList如果「主題」值是FORM或URL參數,這一點尤其重要。注意非整數數值(科學記數法,長整數,小數,數字後跟空格等)。'' –