ASP.NET SQL Select语句,参数

时间:2017-11-19 01:32:09

标签: c# jquery sql asp.net oracle

我正在开发一个ASP.NET页面,并且已经使用搜索框来查询来自Oracle数据库的数据的gridview。截至目前,我有一个参数文本框(“InventorySearchBox”)。这使我能够搜索一列。我添加了另一个参数,一个下拉列表(“InventoryDropDownList”),但我无法弄清楚如何在我的select语句中使用它。基本上,(“InventoryDropDownList”)现在有两个列表项,但可能会增长,基于列表选择,我希望搜索框与不同的列相关。

所以:如果参数(“InventoryDropDownList”)='searchbyproductcode'那么参数(“InventorySearchBox”)= tp.product_code else如果参数(“InventoryDropDownList”)='searchbydescription'那么参数(“InventorySearchBox”)就像tic.product_description

TL; DR:下拉列表定义搜索框等于的列。这是我的代码,在没有下拉列表的情况下工作。我已经尝试过IF语句,BEGIN,END和CASE语句,但还没有想出来。非常感谢任何帮助。

SELECT tp.product_code,
    tic.product_description,
    til.location,
    SUM(trt.inventory_balance) QTY,
    trt.uom
FROM treceipt_transaction trt, tinventory_location til, tinventory_control tic, tproduct tp
WHERE trt.receipt_location = til.location AND
    trt.category_no = tic.category_no AND
    trt.product_description = tic.product_description AND
    trt.uom_schedule_id = tic.uom_schedule_id AND
    trt.uom = tic.uom AND
    tic.category_no = tp.category_no AND
    tic.product_description = tp.product_description AND
    tic.uom_schedule_id = tp.uom_schedule_id AND
    tp.product_code = :product_code AND
    trt.inventory_balance <> 0
GROUP BY tp.product_code, tic.product_description, til.location, trt.uom
ORDER BY til.location

我曾经尝试过这样的事情:

 WHERE trt.receipt_location = til.location AND
        trt.category_no = tic.category_no AND
        trt.product_description = tic.product_description AND
        trt.uom_schedule_id = tic.uom_schedule_id AND
        trt.uom = tic.uom AND
        tic.category_no = tp.category_no AND
        tic.product_description = tp.product_description AND
        tic.uom_schedule_id = tp.uom_schedule_id AND
        IF(:InventoryDropDownList = 'searchbyproductcode' THEN
        tp.product_code = :InventorySearchBox ELSE IF
        :InventoryDropDownList = 'searchbydescription' THEN
        tic.product_description = :InventorySearchBox) AND
        trt.inventory_balance <> 0

先谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用案例来执行此操作:

WHERE
  trt.receipt_location = til.location AND
  trt.category_no = tic.category_no AND
  trt.product_description = tic.product_description AND
  trt.uom_schedule_id = tic.uom_schedule_id AND
  trt.uom = tic.uom AND
  tic.category_no = tp.category_no AND
  tic.product_description = tp.product_description AND
  tic.uom_schedule_id = tp.uom_schedule_id AND
  CASE WHEN :InventoryDropDownList = 'searchbyproductcode' THEN tp.product_code
       WHEN :InventoryDropDownList = 'searchbydescription' THEN tic.product_description
       ELSE :InventorySearchBox
  END = :InventorySearchBox AND
  trt.inventory_balance <> 0

请注意,使用ELSE并:InventorySearchBox = :InventorySearchBox允许此检查“默认”,如果下拉列表中没有选择或无效,则“无效”。