1

I'm working on a jasper report which takes one parameter based on which it should have a different where clause. Below is the query I'm using but seems there's a syntax error somewhere.

  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
         );

I appreciate any help, thanks

4

2 回答 2

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。如果允许,您需要将其添加到逻辑中。

于 2013-05-20T21:29:39.207 回答
1

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

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

2)创建另一个参数说 param2 ,它具有像 $P{param1}.longValue () == 1 这样的 epression ?" 和 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>
于 2013-05-21T17:27:06.363 回答