Dynamic SQL - EXEC(@SQL) versus EXEC SP_EXECUTESQL(@SQL)

Ash Machine picture Ash Machine · Feb 14, 2009 · Viewed 207k times · Source

What are the real world pros and cons of executing a dynamic SQL command in a stored procedure in SQL Server using

EXEC (@SQL)

versus

EXEC SP_EXECUTESQL @SQL

?

Answer

Mitch Wheat picture Mitch Wheat · Feb 14, 2009

sp_executesql is more likely to promote query plan reuse. When using sp_executesql, parameters are explicitly identified in the calling signature. This excellent article descibes this process.

The oft cited reference for many aspects of dynamic sql is Erland Sommarskog's must read: "The Curse and Blessings of Dynamic SQL".