Stored Procedure count of records only once

Rock Hitman 46 Reputation points
2020-09-24T21:34:48.687+00:00

Hi, I am using below stored procedure it works fine,but for each call I want only once the count of records.

how can I achieve this ? How should I modify my code logic. Sometimes the records in the table are not starting from 0
So if Poll is 0....I am missing the count of records. How to fix this ?

ALTER PROCEDURE [dbo].[usp_Test_Select]
     (  

       @Year int,
       @Poll int,
       @count int 
      )


             Select  [ID]
            ,[ID]
            ,[Name]
            ,[City]            
            ,[Year],
            case when [ID] = @Poll*(10-9)  then (select count(*) from [dbo].[TestDB]) end TotalRec 

            from [dbo].[TestDB]
            Where [ID] between ((@Poll) + 1) and (@Poll)*@count   
Developer technologies | Transact-SQL
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2020-09-25T07:41:31.073+00:00

    Maybe redesign your procedure to return two recordsets: the count and the subset of records (page). Something like this:

    ALTER PROCEDURE [dbo].[usp_Test_Select]  
    . . .  
       -- return the total number of rows  
       select count(*) from [dbo].[TestDB]  
       -- return the data  
       Select  [ID]  
                    [Name],  
                    [City],              
                    [Year]  
                    from [dbo].[TestDB]  
                    where [ID] between ((@Poll) + 1) and (@Poll)*@count     
    

    The caller code will need some simple adjustments to deal with two recordsets.

    However, your core approach works only in case of “ideal” IDs, which start from 1 and do not have any gaps (that appear if you delete some rows, for example). This is not always possible. Consider the previous suggestions based on OFFSET or ROW_COUNT: https://learn.microsoft.com/answers/answers/85864/view.html, which can be adjusted to take into consideration the page size (@count).

    Maybe notions like “pagination”, “page size” (instead of @count) and “page number” (replacing @joon ) better describe your needs.

    1 person found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2020-09-24T22:11:04.347+00:00
         ALTER PROCEDURE [dbo].[usp_Test_Select]
              (  
    
                @Year int,
                @Poll int,
                @count int 
               )
    
         DECLARE @tblcnt int = (select count(*) from [dbo].[TestDB]
    
                      Select  [ID]
                     ,[ID]
                     ,[Name]
                     ,[City]            
                     ,[Year],
                     @tblcnt AS TotalRec 
                     from [dbo].[TestDB]
                     Where [ID] between ((@Poll) + 1) and (@Poll)*@count   
    

    Yeah, you will get the rowcount on all rows, but what's the problem with that? (You can arrange to get it one row only, but frankly, it is not worth the complexity.(


  3. Guoxiong 8,206 Reputation points
    2020-09-24T22:18:02.423+00:00

    CASE WHEN @joon = 0 OR [ID] = @joon (10-9) THEN (SELECT COUNT() FROM [dbo].[TestDB]) ELSE NULL END TotalRec


  4. EchoLiu-MSFT 14,621 Reputation points
    2020-09-25T07:19:13.257+00:00

    Hi @Rock Hitman ,

    In your previous post, I have helped you solve this problem, and you have also checked that it is working. I wonder why you asked the same question.
    I still give the same answer:

              ALTER PROCEDURE [dbo].[usp_GeicoData_Select]  
                    (@poll int)  
               AS  
                    Select  [ID]  
                           ,[VIN]  
                           ,[Make]  
                           ,[Model]  
                           ,[Year]  
                ,case when [ID]=@Poll*10-9 then (select count(*) from [dbo].[usp_GeicoData_Select] ) end Ycount  
                           FROM [TEST].[dbo].[Ins_Data]  
                           Where [ID] between @Poll*10-9 and @Poll*10  
                           ORDER BY [ID]  
                                     
              execute [dbo].[usp_GeicoData_Select] @poll=1    
    

    Test code:

      alter procedure b_stu  
      (@poll int)  
      as  
      select EmpID,EmpName,SalaryCalc,case when EmpID=@Poll*10-9 then (select count(*) from dbo.Employee ) end Ycount    
      from dbo.employee   
      where  EmpID between @Poll*10-9 and @Poll*10   
           
      execute b_stu @poll=1  
    

    28321-image.png
    In addition, the following code can display the count on only one line:

             ALTER PROCEDURE [dbo].[usp_Test_Select]  
                  (    
                  
                    @Year int,  
                    @Poll int,  
                    @count int   
                   )  
                       
                       
                          Select  [ID]  
                         ,[ID]  
                         ,[Name]  
                         ,[City]              
                         ,[Year]    
                         from [dbo].[TestDB]  
                         Where [ID] between ((@Poll) + 1) and (@Poll)*@count     
              union all  
                         select * from (select  01 [count1] ,02 [count2] ,'count' [count3],'count' [count4],  
                         case when [ID] = ((@Poll) + 1) then (select count(*) from [dbo].[TestDB] Where [ID] between ((@Poll) + 1) and (@Poll)*@count) end TotalRec     
                         from [dbo].[TestDB]) t  
                         where TotalRec>0  
    

    Test code:

    alter procedure b_stu  
    (@poll int)  
    as  
    select EmpID,EmpName,SalaryCalc  
    from dbo.employee   
    where  EmpID between @Poll*10-9 and @Poll*10   
    union all  
    select * from (select  01 [count1] ,'count' [count2],  
    case when EmpID=@Poll*10-9 then (select count(*) from dbo.Employee where EmpID between @Poll*10-9 and @Poll*10 ) end Ycount     
    from dbo.Employee) t  
    where Ycount>0  
      
    execute b_stu @poll=1  
    

    28250-image.png
    Best Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.