2017-07-13 245 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参数,这一点尤其重要。注意非整数数值(科学记数法,长整数,小数,数字后跟空格等)。'' –