日期:2014-05-17  浏览次数:20401 次

SQL存储过程问题
一个分页多条件查询
SQL code

alter procedure GetByMoreSelect 
(@selectWhere VarChar(1000),
@pagesize int,
@pageindex int)
as

begin
 with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY  id)AS Row, * from vedioWorks O  where '1'=@selectwhere) 
 SELECT * FROM temptbl where Row between (@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize
end


调试得到值 '1' and jishu='实拍'

执行存储过程
SQL code

DECLARE    @return_value int

EXEC    @return_value = [dbo].[GetByMoreSelect]
        @selectWhere = N'''1'' and jishu=''实拍''',
        @pagesize = 9,
        @pageindex = 1

SELECT    'Return Value' = @return_value

GO



 无效 查不到数据,其实是有数据的
请问哪里出了问题,或者怎么改

------解决方案--------------------
ECLARE @return_value int

你的返回值明明是结果集,怎么这里声明成int型
------解决方案--------------------
DECLARE @return_value int

这个应该声明成table而不是int
------解决方案--------------------
返回结果好像不对应吧
------解决方案--------------------
SQL code

ALTER PROCEDURE [dbo].[PROCE_SQL2005PAGECHANGE]
(
 @TableName varchar(50),            --表名
 @ReFieldsStr varchar(200) = '*',   --字段名(全部字段为*)
 @OrderString varchar(200),         --排序字段(必须!支持多字段不用加order by)
 @WhereString varchar(500) =N'',  --条件语句(不用加where)
 @PageSize int,                     --每页多少条记录
 @PageIndex int = 1 ,               --指定当前为第几页
 @TotalRecord int output            --返回总记录数
)
AS
 
BEGIN    

    --处理开始点和结束点
    Declare @StartRecord int;
    Declare @EndRecord int; 
    Declare @TotalCountSql nvarchar(500); 
    Declare @SqlString nvarchar(2000);    
    set @StartRecord = (@PageIndex-1)*@PageSize + 1
    set @EndRecord = @StartRecord + @PageSize - 1 
    SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName;--总记录数语句
    SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
    --
    IF (@WhereString! = '' or @WhereString!=null)
        BEGIN
            SET @TotalCountSql=@TotalCountSql + '  where '+ @WhereString;
            SET @SqlString =@SqlString+ '  where '+ @WhereString;            
        END
    --第一次执行得到
    --IF(@TotalRecord is null)
    --   BEGIN
           EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回总记录数
    --  END
    ----执行主语句
    set @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' +  ltrim(str(@EndRecord));
    Exec(@SqlString)    
END