SQLServer Top语句参数化方法
程序员文章站
2023-12-04 17:31:58
declare @topcount int set @topcount = 100 select top (@topcount) * from adventureworks...
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
上一篇: C#自定义导出数据到Excel的类实例
下一篇: 数据库 日期加减处理
推荐阅读
-
SQLServer Top语句参数化方法
-
SqlServer参数化查询之where in和like实现详解
-
SqlServer参数化查询之where in和like实现之xml和DataTable传参介绍
-
sqlserver 多表关联时在where语句中慎用trim()方法
-
Python读取txt文件数据的方法(用于接口自动化参数化数据)
-
关于静态语句块、非静态语句块,成员变量初始化、构造方法在父子类执行的顺序:
-
SQLServer Top语句参数化方法
-
jmeter如何参数化?Jmeter参数化设置的5种方法
-
PHP获取当前日期和时间及格式化方法参数
-
PHP实现SQL语句格式化功能的方法 原创