谁帮我解释下这个存储过程相关的疑问

cngothic cngothic 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

CREATE proc getdataset
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(30), --搜索的表名
@SelectWhere Varchar(500)='',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主键字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
as

declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600)

set nocount on--关闭计数

set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere

execute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount OUTPUT

if (@RecordCount = 0) --如果没有贴子,则返回零
return 0

/*判断页数是否正确*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO

问题
1:这个存储过程效率高吗?我测试37W数据的情况下翻页速度一般
2:

set nocount on

set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere

execute sp_executesql
@TmpSelect, N'@RecordCount int OUTPUT' ,
@RecordCount OUTPUT

if (@RecordCount = 0) return 0

if (@intPageNo - 1) * @intPageSize > @RecordCount return (-1)
set nocount off--打开计数

set nocount on, set nocount off。关闭-打开 受SQL语句影响的行
set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere
这句属于影响影响行的SQL语句吗?
set nocount on, set nocount off 一会关闭一会打开有什么用尼?

3:
sp_executesql 这个存储过程怎么用的尼。查了些资料米看懂。就上面的例子而言带有三个参数

@TmpSelect,
N'@RecordCount int OUTPUT' ,
@RecordCount OUTPUT

朋友帮我解释下。还有N’是什么意思?

4:
execute sp_executesql @TmpSelect
return(@@rowcount)

整个存储过程执行完输出受影响的行数。
整个过程有三处返回值。还有一个OUTPUT 输出变量。共四个值了有点迷糊return 返回的值在.cs文件里面怎么获取尼?
获取记录总数的是return(@@rowcount) 还是 @RecordCount OUTPUT尼。看了与此存储过程相对应的.CS。是用的@RecordCount 返回的记录总数。


...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
cngothic
MS-SQL Server 34,876 成员 254,635 内容