加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLServer Top语句参数化方法

发布时间:2020-12-12 09:36:26 所属栏目:MsSql教程 来源:网络整理
导读:declare @TopCount int set @TopCount = 100 select top (@TopCount) * from AdventureWorks.HumanResources.Employee 如果有Like等字句,一定要拼Sql的话,也应该使用sp_executesql来执行,示例如下: declare @TopCount int --定义top 数量 set @TopCount

declare @TopCount int
set @TopCount = 100
select top (@TopCount) * from AdventureWorks.HumanResources.Employee 如果有Like等字句,一定要拼Sql的话,也应该使用sp_executesql来执行,示例如下: declare @TopCount int --定义top 数量
set @TopCount = 100
declare @Title nvarchar(100) --定义like内容
set @Title = '%n%'
declare @SelectSql nvarchar(max)
set @SelectSql = '
select top (@TopCountPar) *
from AdventureWorks.HumanResources.Employee
where Title like @TitlePar' --使用参数化的top和like --使用sp_executesql 来执行,可以提高效率
exec sp_executesql @SelectSql,
N'@TopCountPar as int,@TitlePar as nvarchar(100)',
@TopCountPar = @TopCount,@TitlePar = @Title

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读