Dynamic Query (Execute or sp_executesql ) vs. static Query

'Whether we should use static or dynamic SQL statements does NOT relate to an IF statement.

When choosing between static / dynamic SQL statements, you are suggested to use static SQL statements (usp_ListWorkOrder02) for most of the cases. Dynamic SQL statements are usually used when static SQL statements cannot fulfill your needs. Thus, use dynamic SQL statements (usp_ListWorkOrder01) only when you do NOT know the table names or the column names for a particular statement, or when the selection conditions (WHERE) is too complicated to be handled in a static way. (while there are still alternatives / workarounds for not using dynamic SQL statements.)'  - http://social.microsoft.com/Forums/zh-HK/sqlserverzhcht/thread/012e75f8-1b92-4d2a-892a-915fe45b1e41

 

 

Question 1 : Use static or dynamic sql query ?

  • Mostly use static
  • Use dynamic sql query at below two cases
  1. Not know the table names
  2. Not know the column names

Question 2 : Use execute or sp_executesql when we are using dynamic sql query?

 

  • different between execute and sp_executesql
  1. sp_executesql has input & output
  2. sp_executesql can pass parameter - avoid sql injection
  3. sp_executesql can cached execute plan
  4. Mostly use sp_executesql

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值