Concatenate Wheres in a Stored Procedures

-2

Could you help me create a query through a Stored Procedure? to select Sales with:

  • pClient (only when it is not null)

  • pProduct (only when it is not null)

  • pSeller (only when it is not null)

  • List of Sales (p From, PHase, Customer, Product, Salesperson)

    ifnull pCliente then cSqlCliente="" else cSqlCliente="and Cliente= pCliente "
    fnull pProducto then cSqlProducto="" else cSqlProducto="and Producto= pProducto "
    ifnull pVendedor then cSqlVendedor="" else cSqlVendedor="and Vendedor= pVendedor "
    cSqlSelect:= "Select * from Ventas where fecha between pDesde and pHasta "+
     cSqlCliente+ 
     cSqlProducto+
     cSqlVendedor
    Exec( cSqlSelect)
    
asked by Luis Torres La Madrid 18.04.2018 в 19:56
source

1 answer

0

It can be in a single query without having to dynamically generate the query.

It would be more or less like that for the client but it would be a similar logic for N variables

Select * from Ventas where fecha between pDesde and pHasta 
AND 
(
     pCliente is null 
    or  Cliente= pCliente
);
    
answered by 18.04.2018 / 20:19
source