SQLServer Top语句参数化方法_mssql数据库教程-查字典教程网
SQLServer Top语句参数化方法
SQLServer Top语句参数化方法
发布时间:2016-12-29 来源:查字典编辑
摘要:declare@TopCountintset@TopCount=100selecttop(@TopCount)*fromAdventureW...

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

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新mssql数据库学习
    热门mssql数据库学习
    编程开发子分类