0

Trying to create a Stored procedure that will give the required no. of records and also the total no. of records in that table.

The '@Query' part gives result when I execute it separately. So there's no problem with that. Could Someone please check if the syntax is correct, cause I'm not getting at any result.

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'

Otherwise, the queries would be:

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;
4
  • you are assigning int to @TotalRows which is of type nvarchar Commented Nov 30, 2013 at 9:46
  • If assigning to a NVARCHAR variable, you should use the N'...' to indicate Unicode. Next: you must put a semicolon (;) before the with keyword. Commented Nov 30, 2013 at 9:47
  • write query like select @Query = 'select count(*) .... Commented Nov 30, 2013 at 9:47
  • @Terror.Blade. How Should I store it in a variable then. Commented Nov 30, 2013 at 9:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

You have declared it as int in paramDefinition. The first declaration was nvarchar. So shold I also change that to int. Why is there an extra comma/param after @query in the exec statement. I'm assuming that the final execute statement to test the procedure is correct...is it
execute GetRecordsByPage 'tblBranch','BranchName', '2', '10', /@TotalRows output - gives an error- must declare scalar variable /@TotalRows - But isnt it already declared
y are you putting /@totalrows use GetRecordsByPage 'tblBranch','BranchName', '2', '10', @TotalRows
When using just '@'...it isnt allowing to submit. It thinks thts a username. The line's just as you wrote, but with 'output at the end
how you call it is it c#.. i am just asking
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.