具有另一个sql语句的where子句中的case条件

时间:2013-05-20 21:21:18

标签: sql oracle jasper-reports case

我正在研究一个jasper报告,该报告采用一个参数,根据该参数,它应该有一个不同的where子句。下面是我正在使用的查询,但似乎某处存在语法错误。

  select customer.name
         customer_order.name
  from customer, customer
  where customer.orders > 0
    and customer_order.customer_id = customer.id
    and customer_order.name in
        (
           case when (<paramter> = 1) then ('order1', 'order2')
           else  (select order_name from customer_order)
           end
         );

我感谢任何帮助, 感谢

2 个答案:

答案 0 :(得分:2)

您无法像这样编写查询。这是另一种选择:

where . . .
      ((<parameter> = 1) and customer_order.name in ('order1', 'order2') or
       (<parameter> <> 1) and customer_order.name in (select order_name from customer_order)
      )

这假设<parameter>不是NULL。如果允许,则需要将其添加到逻辑中。

答案 1 :(得分:1)

您可以使用参数作为占位符动态创建查询的任何部分。

1)假设您有参数param1,它是实际参数

2)创建另一个参数说param2,其中包含epression     $ P {param1} .longValue()== 1         ? “和field1 =”         :“和field1 =”

3)在查询中使用param2作为占位符

    Select 
        blah blah
    From 
        blah blha
    Where
        blah blah
        $P!{param2}

现在,根据你的where子句可能看起来像的param1值

        Where 
            blah blah
            and field1=  <exp1>
        or 

         Where 
            blah blah
            and field1=  <exp2>
相关问题