SQLServer Top语句参数化方法

2023年 4月 16日 17.8k 0

declare @TopCount int set @TopCount = 100 select top (@TopCount) * from AdventureWorks.HumanResources.Employee 如果有Like等字句,一定要拼Sql的话,也应该使用sp_executesql来执行,示例如下: declare @TopCount int --定

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

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论