Please tell me what is difference
==> if i write query directly in storedprocedure
==> and write query in string variable and than run it in exec in stored procedure.
i am using ms sql server 2005
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
With some exceptions
EXEC('sql stmnt')is what you use when you have no other choice.It allows you to dynamically build a statement and execute it, which is often the only way of achieving something when object names are variable and not known in advance.
Read this article on dynamic SQL which explains scenarios when/why dynamic SQL is useful & goes into detail about
EXEC().As for the differences between running an SQL statement in a stored procedure and running it in the procedure as
EXEC(@SQL_STRING):@SQL_STRINGwill be checked@SQL_STRINGis within its own scope relative to the SP@SQL_STRINGwhich can lead to security problems.@SQL_STRINGwill be cached but only reused if a subsequentEXEC(@SQL_STRING)matches it exactly, with an SP a single query plan can be reused if all that changes are parameters.