0

Who is more efficient in a stored procedure

an IF ... ELSE to change the where clause or add a condition in where to evaluate if put a condition

Examples.

SELECT *
FROM table
WHERE monto = 100 
    AND ( @canal IS NULL OR canal = @canal )

OR

IF @canal IS NULL
BEGIN
    SELECT *
    FROM table
    WHERE monto = 100
END
ELSE
BEGIN
    SELECT *
    FROM table
    WHERE monto = 100
        AND canal = @canal

In personal opinion, without being an expert I think the first example is the best option, but I not have a lot of experience in sql

Or maybe exist another best solution.

Please help me, because I wanna optimize a stored-procedure

Felipe Pincheira
  • 442
  • 1
  • 6
  • 21
  • 1
    http://stackoverflow.com/questions/3415582/how-can-i-use-optional-parameters-in-a-t-sql-stored-procedure and http://stackoverflow.com/questions/532468/ignoring-a-null-parameter-in-t-sql – Tim Schmelter Feb 25 '16 at 15:15
  • This has the look of the beginnings of a catch all query. See this article for some great ideas on dealing with this type of thing. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/ – Sean Lange Feb 25 '16 at 15:25

0 Answers0