IF in Storedprocedure within a select, remove sp_executesql @query

Asked

Viewed 209 times

2

I have a Storedprocedure that unfortunately only because of an IF has ceased to be a select and turned everything into a VARCHAR statement and then I run it, losing one of the main benefits of MSSQL which is the compilation.

I modified the SP only to illustrate the problem:

 ALTER PROCEDURE [dbo].[stp_Admin14_listagem_impressao]
        @intacao int,
        @IDCorretor int
    AS
    BEGIN

    declare @query nvarchar(max)

    set @query = 'SELECT  * from exemplo
    WHERE     intacao = '+  cast(@intacao as varchar) +') 

--// #### AQUI é o problema, o que fazer???
    if (@IDCorretor <> 0)
    BEGIN
        set @query  = @query + ' AND tbl_imoveis.int_CORRETOR = ' + cast(@IDCorretor as varchar)
    END


    exec sp_executesql @query
    END

In short, if you see the @Idcorretor <> 0 parameter I should add a Where clause

1 answer

1


You can always keep the condition in the query, including a OR for the parameter; this eliminates the IF.

It goes something like this:

...AND (@IDCorretor = 0 OR tbl_imoveis.int_CORRETOR = @IDCorretor)

Then the stored complete trial would look like this:

ALTER PROCEDURE [dbo].[stp_Admin14_listagem_impressao]
    @intacao int,
    @IDCorretor int
AS
BEGIN
    SELECT * from exemplo WHERE intacao = @intacao as varchar
    AND (@IDCorretor = 0 OR tbl_imoveis.int_CORRETOR = @IDCorretor)
END

Note the parentheses I added. Don’t forget them!

A remark

(...) losing one of the main benefits of MSSQL which is the compilation.

You don’t miss any performance benefits by using sp_executesql but rather for concatenating the parameters in the string instead of using them as the actual parameter.

See how to pass your parameters instead of concatenating them in the query: sp_executesql (Transact-SQL).

Thus the query execution plan will be reused in subsequent executions when there has been a change only in the value of the parameters.

  • I usually use sp_executesql when sending an array, so I command it in text format and concatenate everything by assembling a string and executing it...

  • Your code seems to work, but I still don’t understand how it actually worked.

  • 1

    Basically I passed the logic of IF into the query. Yours IF says: "if the reported broker is non-zero, then select the results based on this broker". My OR does more or less the same thing: "bring the correctors equal to the parameter or, if the parameter is zero, it does not matter the corrector". I was able to explain?

  • Yes, thank you. It would be nice to post the code as it was: ALTER PROCEDURE [dbo]. [stp_Admin14_listagem_impressao]&#xA; @ intacao int,&#xA; @ IDCorretor int&#xA; AS&#xA; BEGIN&#xA;&#xA;&#xA;SELECT * from exemplo&#xA; WHERE intacao = @intacao&#xA;AND (@IDCorretor = 0 OR tbl_imoveis.int_CORRETOR = (@IDCorretor))

  • the Where clause was wrong at the end pq you cast, I used the cast only for concatenation conversion... intacao = @intacao AND (@Idbroker = 0 OR tbl_imoveis.int_CORRETOR = (@Idbroker))

  • @Dorathoto Certo. I was really weirded out by these Casts but I didn’t want to get into the merit :-)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.