我的sql存储过程是执行分页查询的 返回当前页的数据datatable 和output输出参数总条数pagecount能一条sql

CREATEPROCEDUREp_FindImages@nikeNamenvarchar(20),@imageTypeint,@imageTitlenvarchar(15... CREATE PROCEDURE p_FindImages
@nikeName nvarchar(20)
,@imageType int
,@imageTitle nvarchar(150)
,@bTime varchar(30)
,@eTime varchar(30)
,@state int
,@bATime varchar(30)
,@eATime varchar(30)
,@loop bit
,@page int
,@pageSize int
,@pages int output
WITH ENCRYPTION
AS
SET NOCOUNT ON
BEGIN TRY
SELECT @pages=COUNT(*) FROM v_GWPlayerImage AS A
WHERE (@nikeName IS NULL OR A.NikeName like '%'+@nikeName+'%')
AND (@imageType IS NULL OR A.ImageType=@imageType)
AND (@imageTitle IS NULL OR A.ImageTitle like '%'+@imageTitle+'%')
AND (@bTime IS NULL OR A.UploadDate>=DBO.f_FormatFromDate(@bTime))
9 AND (@eTime IS NULL OR A.UploadDate<=DBO.f_FormatEndDate(@eTime))
AND (@state is null OR A.[Status]=@state)
AND (@bATime IS NULL OR A.AuditDate>=DBO.f_FormatFromDate(@bATime))
AND (@eATime IS NULL OR A.AuditDate<=DBO.f_FormatEndDate(@eATime))
AND (@loop IS NULL OR A.AddToLoopPlay=@loop)

SELECT * FROM (
SELECT A.*,ROW_NUMBER() over (order by A.ImageID desc) ROWNUMBER
FROM v_GWPlayerImage AS A
WHERE (@nikeName IS NULL OR A.NikeName like '%'+@nikeName+'%')
AND (@imageType IS NULL OR A.ImageType=@imageType)
AND (@imageTitle IS NULL OR A.ImageTitle like '%'+@imageTitle+'%')
AND (@bTime IS NULL OR A.UploadDate>=DBO.f_FormatFromDate(@bTime))
AND (@eTime IS NULL OR A.UploadDate<=DBO.f_FormatEndDate(@eTime))
AND (@state is null OR A.[Status]=@state)
AND (@bATime IS NULL OR A.AuditDate>=DBO.f_FormatFromDate(@bATime))
AND (@eATime IS NULL OR A.AuditDate<=DBO.f_FormatEndDate(@eATime))
AND (@loop IS NULL OR A.AddToLoopPlay=@loop)

) AS TEMP
WHERE ROWNUMBER>(@page-1)*@PageSize AND ROWNUMBER<=@page*@PageSize;
能一条sql即返回datatable 又有输出参数pagecount么 就是用一条sql了 不用存储过程了可以么求大神帮忙
展开
 我来答
xia411123129
2013-04-09 · 超过12用户采纳过TA的回答
知道答主
回答量:52
采纳率:100%
帮助的人:15.6万
展开全部
给你个我在用的分页存储过程
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER proc [dbo].[global.pagebreak]
@pFields varchar(4000)='*',--查询的字段
@pSortField varchar(2000)='',--排序的字段
@pTable varchar(3000)='',--所查的表
@pWhere varchar(4000)='',--条件
@pOnePageRows int = 25,--每页显示的条数
@pIndex int = 1
as
declare @s varchar(5000)
if (@pWhere='' or @pWhere=NULL)
begin
set @s ='
DECLARE @totalRows int
DECLARE @totalPages int
SELECT @totalRows = COUNT(*) From '+@pTable+'
SET @totalPages = @totalRows / '+cast(@pOnePageRows as varchar)+'
IF( @totalPages * '+cast(@pOnePageRows as varchar) + '< @totalRows)
SELECT @totalPages = @totalPages + 1
SELECT '+@pFields+' FROM(
SELECT ROW_NUMBER() OVER(order by '+@pSortField+') as ROWID, '+@pFields+' From '+@pTable+')
AS TSP
WHERE ROWID BETWEEN '+cast((((@pIndex-1)*@pOnePageRows)+1) as varchar)+'
AND '+cast((@pIndex*@pOnePageRows) as varchar)+'
SELECT @totalRows AS TOTALROWS,
@totalPages AS TOTALPAGES,
'+cast(@pIndex as varchar)+' AS CURRENTPAGE,
'+cast(@pOnePageRows as varchar)+' AS ONEPAGEROWS '
end
else
begin
set @s ='
DECLARE @totalRows int
DECLARE @totalPages int
SELECT @totalRows = COUNT(*) From '+@pTable+' where '+@pWhere+'
SELECT @totalPages = @totalRows / '+cast(@pOnePageRows as varchar)+'
IF( @totalPages * '+cast(@pOnePageRows as varchar) + '< @totalRows)
SELECT @totalPages = @totalPages + 1
SELECT '+@pFields+' FROM(
SELECT ROW_NUMBER() OVER(order by '+@pSortField+') as ROWID, '+@pFields+' From '+@pTable+' where '+@pWhere+')
AS TSP
WHERE ROWID BETWEEN '+cast((((@pIndex-1)*@pOnePageRows)+1) as varchar)+'
AND '+cast((@pIndex*@pOnePageRows) as varchar)+'
SELECT @totalRows AS TOTALROWS,
@totalPages AS TOTALPAGES,
'+cast(@pIndex as varchar)+' AS CURRENTPAGE,
'+cast(@pOnePageRows as varchar)+' AS ONEPAGEROWS '
end
print @s
exec(@s)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
传奇勇者
2013-04-09 · TA获得超过747个赞
知道大有可为答主
回答量:1706
采纳率:75%
帮助的人:798万
展开全部
你这个存储过程已经够烂了,还要用一句来实现,你这个思路反了,既麻烦又没效率。
更多追问追答
追问
谢谢  我只是想问 一条sql能实现上面我的需求么 即返回数据集 又能获取到输出参数
追答
你这样的逻辑,神仙也迷路了,肯定实现不了了。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式