带输出参数的存储过程

时间:2013-11-30 09:45:19

标签: sql-server tsql stored-procedures

尝试创建一个将提供所需编号的存储过程。记录和总数没有。该表中的记录。

' @ Query'当我单独执行时,part给出结果。所以没有问题。 有人可以检查语法是否正确,因为我没有得到任何结果。

ALTER PROCEDURE GetRecordsByPage    
(    
  @TableName nvarchar(50),    
  @ColumnName nvarchar(50),    
  @OrderByClause nvarchar(50),    
  @StartIndex nvarchar(50),    
  @EndIndex nvarchar(50),    
  @TotalRows nvarchar(50) output    
)    
AS    
BEGIN    
  DECLARE @Query nvarchar(max)    

  select @Query = 'select '+@TotalRows+' = count(*) from '+@TableName+' 
                   where deleted=0

                   with temp as (select row_number() over (order by '+    
                   @colname+' '+@OrderByClause+') as row, * from '+@tablename+')

                   select * from temp where row between '+@startIndex+' and '
                   +@EndIndex    

   execute sp_executesql @Query, @TotalRows output 

END
--is this correct, to test this stored procedure
-- execute GetRecordsByPage 'tblBranch','BranchName', '2', '10'

否则,查询将是:

select count(*) from tblBranch;
With temp as
(select row_number over(order by name desc) as row, * from tblbranch)
select * from temp where row between 11 and 20;

1 个答案:

答案 0 :(得分:1)

ALTER PROCEDURE GetRecordsByPage    
(    
  @TableName nvarchar(50),    
  @ColumnName nvarchar(50),    
  @OrderByClause nvarchar(50),    
  @StartIndex nvarchar(50),    
  @EndIndex nvarchar(50),    
  @TotalRows nvarchar(50) output    
)    
AS    
BEGIN    
  DECLARE @Query nvarchar(max)    

  select @Query = 'select @TotalRowsOut = count(*) from '+@TableName+' 
                   where deleted=0

                   with temp as (select row_number() over (order by '+    
                   @colname+' '+@OrderByClause+') as row, * from '+@tablename+')

                   select * from temp where row between '+@startIndex+' and '
                   +@EndIndex    

    DECLARE @ParmDefinition nvarchar(500);
SET @ParmDefinition = N'@TotalRowsOut int OUTPUT';
       execute sp_executesql @Query,  @ParmDefinition,@TotalRowsOut=@TotalRows output 
select @TotalRows
END