开发者

PL/SQL optional where [duplicate]

开发者 https://www.devze.com 2023-03-03 08:48 出处:网络
This question already has answers here: Stored Procedure with optional "WHERE" parameters (6 answers)
This question already has answers here: Stored Procedure with optional "WHERE" parameters (6 answers) Closed 11 months ago.

Hey, I have this query in PL/SQL:

--Ver todos los atributos de las OL de una OS.
SELECT attr.swspattrdataid attr_data_id,
       att.swname attribute_swname,
       attr.swvalue attr_data_swvalue
  FROM sw_sp_attr_data attr, 
       sw_sp_attribute att
 WHERE swobjectid IN (SELECT swsporderlineid
                        FROM sw_sp_order_line
                       WHERE swsporderid = 21444963 --Orden 
                       **AND swsporderlineid = a_number**
                     );
   AND att.swspattributeid = attr.swspattributeid
 --AND att.swname LIKE '%%'                          --Filtrar por nombre
开发者_如何学运维

I need to have the AND filter between ** to be optional, so no matter if I put a number there the query runs OK, is this posible?

Thanks!


Declare the parameter with a default value of NULL. So in case it is not passed to the procedure it will automatically be null.

Then change the condition to:

AND (a_number IS NULL OR swsporderlineid = a_number)


You can replace the swsporderlineid = a_number clause with this OR clause:

   WHERE      swobjectid IN (SELECT swsporderlineid
                             FROM sw_sp_order_line
                             WHERE swsporderid = 21444963 --Orden 
                               AND (swsporderlineid = a_number OR a_number IS NULL));

Therefore, if a_number is null, the second line evaluates to true for all records, allowing the query to continue.

0

精彩评论

暂无评论...
验证码 换一张
取 消