34,590
社区成员
发帖
与我相关
我的任务
分享
ALTER procedure [dbo].[test]
(
@pageIndex int,--当前第几页
@pageSize int,--页容量
@tid int ,
@o varchar(50)
)
as
declare @Count int
exec('
select * from (
select row_number() over (order by '+@o+' ) as RowIndex,
*
from JG_SubjectInfo
where
('+@tid+' = -1 or [tid]='+@tid+')
) as temp
where rowindex>('+@pageIndex+'-1) * '+@pageIndex+' and rowIndex<='+@pageIndex+'*'+@pageSize+'
select '+@Count+' = select count(*) from JG_SubjectInfo
where ('+@tid+' = -1 or [tid]='+@tid+')
return '+@Count+'
')
ALTER procedure [dbo].[test]
(
@pageIndex int,--当前第几页
@pageSize int,--页容量
@tid int ,
@o varchar(50),
@count int output
)
as
declare @sql nvarchar(1000)
set @sql = ('
select * from (
select row_number() over (order by '+@o+' ) as RowIndex,
*
from JG_SubjectInfo
where
('+@tid+' = -1 or [tid]='+@tid+')
) as temp
where rowindex>('+@pageIndex+'-1) * '+@pageIndex+' and rowIndex<='+@pageIndex+'*'+@pageSize+'
select @Count = select count(*) from JG_SubjectInfo
where ('+@tid+' = -1 or [tid]='+@tid+')'
exec sp_executesql @sql,N'@Count int output',@count output
go
--执行
declare @count int
exec [test] 1,30,1,'o',@count output
這樣測測
Create procedure [dbo].[test]
(
@pageIndex int,--当前第几页
@pageSize int,--页容量
@tid int ,
@o varchar(50)
)
as
declare @Count INT
DECLARE @sql NVARCHAR(4000)
SET @sql=' select * from (
select row_number() over (order by '+@o+' ) as RowIndex,
*
from JG_SubjectInfo
where
('+rtrim(@tid)+' = -1 or [tid]='+rtrim(@tid)+')
) as temp
where rowindex>'+RTRIM((@pageIndex-1)* +@pageIndex)+' and rowIndex<='+rtrim(@pageIndex*@pageSize)+'
select @Count = count(*) from JG_SubjectInfo
where ('+rtrim(@tid)+' = -1 or [tid]='+rtrim(@tid)+')'
EXECUTE sp_executesql @sql,N'@Count int output',@Count OUTPUT
return @Count
ALTER procedure [dbo].[test]
(
@pageIndex int,--当前第几页
@pageSize int,--页容量
@tid int ,
@o varchar(50),
@count int output --最好把这个当作输出变量,而不用return
)
as
begin
--declare @Count int
declare @sql nvarchar(max)
set @sql='
select * from (
select row_number() over (order by '+@o+' ) as RowIndex,
*
from JG_SubjectInfo
where
('+@tid+' = -1 or [tid]='+ltrim(@tid)+')
) as temp
where rowindex>('+ltrim(@pageIndex)+'-1) * '+ltrim(@pageIndex)+' and rowIndex<='+ltrim(@pageIndex)+'*'+ltrim(@pageSize)+'
select @Count = select count(*) from JG_SubjectInfo
where ('+ltrim(@tid)+' = -1 or [tid]='+ltrim(@tid)
exec SP_EXECUTESQL @sql,N'@count int output ',@count output
end
ALTER procedure [dbo].[test]
(
@pageIndex int,--当前第几页
@pageSize int,--页容量
@tid int ,
@o varchar(50),
@count int output --最好把这个当作输出变量,而不用return
)
as
begin
--declare @Count int
declare @sql nvarchar(max)
set @sql='
select * from (
select row_number() over (order by '+@o+' ) as RowIndex,
*
from JG_SubjectInfo
where
('+@tid+' = -1 or [tid]='+ltrim(@tid)+')
) as temp
where rowindex>('+ltrim(@pageIndex)+'-1) * '+ltrim(@pageIndex)+' and rowIndex<='+ltrim(@pageIndex)+'*'+ltrim(@pageSize)+'
select @Count = select count(*) from JG_SubjectInfo
where ('+ltrim(@tid)+' = -1 or [tid]='+ltrim(@tid)
exec SP_EXECUTESQL @str,N'@count int output ',@count output
end