简体   繁体   English

Where 子句 SQL (Oracle) 中的案例

[英]Case in Where Clause SQL (Oracle)

I want to filter a query in the Where Statement.我想过滤 Where 语句中的查询。 I want to compare IDs with 'IN' but only if an input has value.我想将 ID 与“IN”进行比较,但前提是输入具有值。 If this text comes empty I don't want to compare.如果此文本为空,我不想进行比较。 Prevent the error query防止错误查询

EXAMPLE:例子:

StringProductIDs = ''



WHERE
    ({Issue}.[ServiceRequestId] IN (@StringServiceRequestIDs)) 
    AND ({ServiceRequest}.[Id] IN (@StringServiceRequestIDs))
    AND ({Product}.[Id] IN (@StringProductIDs) OR @StringProductIDs = '')   /* Is this one that i want to prevent*/
    AND EXTRACT(MONTH FROM {Sprint}.[CompleteDate]) = @Month
    AND EXTRACT(YEAR FROM {Sprint}.[CompleteDate]) = @Year

In this case, I just want to filter things.id if the value passed as a parameter has value.在这种情况下,如果作为参数传递的值具有值,我只想过滤 things.id。 If it comes empty I don't want to compare and avoid the 'AND' line如果它是空的,我不想比较并避免使用“AND”行

Or try another approach:).或者尝试另一种方法:)。 There is a way to run a query only if this input has value?有没有一种方法可以仅在此输入具有值时运行查询? If it has no value, it returns nothing.如果它没有值,则不返回任何内容。 If the input has value run normally –如果输入的值正常运行 –

Many thanks非常感谢

You need OR :你需要OR

WHERE  (things.active = 1 AND things.state = 2) AND
       (things.id IN (@INPUT) OR @INPUT IS NULL); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM