当前位置: 移动技术网 > IT编程>数据库>MSSQL > SQLServer Top语句参数化方法

SQLServer Top语句参数化方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

教师涨工资,闹鬼老宅,电压表原理图

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网