SQLServer Top語句參數(shù)化方法
declare @TopCount int
set @TopCount = 100
select top (@TopCount) * from AdventureWorks.HumanResources.Employee
如果有Like等字句,一定要拼Sql的話,也應(yīng)該使用sp_executesql來執(zhí)行,示例如下:
declare @TopCount int --定義top 數(shù)量
set @TopCount = 100
declare @Title nvarchar(100) --定義like內(nèi)容
set @Title = '%n%'
declare @SelectSql nvarchar(max)
set @SelectSql = '
select top (@TopCountPar) *
from AdventureWorks.HumanResources.Employee
where Title like @TitlePar' --使用參數(shù)化的top和like
--使用sp_executesql 來執(zhí)行,可以提高效率
exec sp_executesql @SelectSql,
N'@TopCountPar as int,@TitlePar as nvarchar(100)',
@TopCountPar = @TopCount,@TitlePar = @Title
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。