Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

What is the difference between EXEC and sp_executesql ?

kishore_peddikishore_peddi Member Posts: 176

Hi,

Please let me know what is the difference between EXEC command and sp_executesql ?

Thanks,
Kishore

Comments

  • diegonzabdiegonzab Member Posts: 6
    :
    : Hi,
    :
    : Please let me know what is the difference between EXEC command and sp_executesql ?
    :
    : Thanks,
    : Kishore
    :

    The main difference is that sp_executesql allows you to include parameters in your query without the need to make any conversion:

    DECLARE @datevar datetime
    ...
    EXEC sp_executesql N'select * from T1 where T1.date1= @datevar', N'@datevar datetime', @datevar
    ...
    this will let you run your query with parameters.

    Second, if you're planning to run a query, (or very similar queries)several times, then it's more efficient to run it with sp_executesql, since the EXEC will recompile the query every time, while sp_executesql will, very probably, use the same execution plan for each execution
Sign In or Register to comment.