Domanda

Ho appena imparato a conoscere la nuova stored procedure sp_executesql. Mi piace il fatto che ci sia un modo per eseguire codice con parametri dall'interno di SQL Server.

Ma, mi chiedo quale sia la differenza tra l'utilizzo della procedura sp_executesql memorizzato quando non si dispone di alcun parametro rispetto a solo chiamando direttamente EXEC. Inoltre, c'è un'implicazione prestazioni?

exec('select * from line_segment')
exec sp_executesql N'select * from line_segment'

Inoltre, c'è una differenza tra il 2005 e il 2008 o se invece gestire questi lo stesso?

È stato utile?

Soluzione

sp_executesql supports parameterisation, whereas EXEC only accepts a string.

Only performance differences that may arise are due to the parameterisation i.e. a parameterised sp_executesql call is more likely to have a reusable cached plan. An EXEC call is likely to lead to lots of single use space wasters in the plan cache.

Altri suggerimenti

This helped me to crack the interview that's why I'm posting to help someone for the same.

enter image description here

Good luck!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a dba.stackexchange
scroll top