简体   繁体   English

如何在SQL Server中提取记录之前对总数进行计数

[英]How to Count Total records before Fetching records in sql server

i am working on windows Form application and wanted to perform Paging in DataGridView so i wrote a query for that 我在Windows Form应用程序上工作,想在DataGridView执行分页,所以我为此写了一个查询

Select  DISTINCT  TOP(1000)
        TblStudentBioData.RegNo
    , Coalesce(TblStudentBioData.First_NameUr + SPACE(1) + 
      TblStudentBioData.Middle_NameUr + SPACE(1),TblStudentBioData.First_NameUr)
    + TblStudentBioData.Last_NameUr AS Name
    , TblStudentBioData.Father_NameUr
    , Coalesce(Ay.AcademicYearName,N'+@InCaseNull+') As AcademicYearName
    , Coalesce(Smst.SemName,N'+@InCaseNull+') As SemName
    , Coalesce(Colg.CollegeName,N'+@InCaseNull+') As CollegeName
    , Coalesce(CID.ClassName,N'+@InCaseNull+') As ClassName
    , TblImages.Images
    , TblStudentBioData.Student_ID
    , TblImages.ImageId
    , Ay.AcademicYearId
    , Smst.SemesterId
    , TblClassSchedule.ClassSchId


FROM TblStudentBioData
        INNER JOIN TBLCFGSEX AS sex
                    ON TblStudentBioData.CfgSexId = sex.CfgSexId
        LEFT JOIN TBLMARITALSTATUS ms
                    ON TblStudentBioData.MaritalStatusId = ms.MaritalStatusId
        INNER JOIN TblImages
                    ON TblStudentBioData.ImageId = TblImages.ImageId
        LEFT JOIN TBLBLOODGROUP BG
                    ON TblStudentBioData.BloodID = BG.BloodId

        LEFT JOIN TblStudentDetail
                    ON (TblStudentBioData.Student_ID = TblStudentDetail.Student_ID)
        LEFT JOIN TblStudentSubAss
                    ON TblStudentDetail.StudentDetailID = TblStudentSubAss.StudentDetailID
        LEFT JOIN TblClassSchedule
                    ON TblStudentDetail.CLassSchID = TblClassSchedule.ClassSchID

        LEFT JOIN TblSubAss
                    ON TblSubAss.SubAssId = TblStudentSubAss.SubAssId
        LEFT JOIN TblClass AS CID
                    ON TblClassSchedule.ClassID = CID.ClassID
        LEFT JOIN TBLCOLLEGE Colg
                    ON CID.CollegeId = Colg.CollegeID
        LEFT JOIN TblSemAssigning SA
                    ON TblClassSchedule.SemAssId = SA.SemAssId
        LEFT JOIN TblAcademicYear AY
                    ON SA.AcademicYearId = AY.AcademicYearId
        LEFT JOIN TblSemester Smst
                    ON Smst.SemesterId = SA.SemesterId

This Query Return me more than 28K rows but for paging i want only 1000 rows and total records which is 28K i will be fetching records 1000 wise. 该查询返回的行数超过28K但对于分页,我只希望1000行,而总记录为28K ,则我将明智地获取记录1000 Any Idea how can i do this ? 任何想法我该怎么做?

Note: 注意:

This query takes more then 20 seconds to execute and i wanted a solution which does not increase executing time. 该查询需要20秒钟以上的时间来执行,我想要一个不会增加执行时间的解决方案。

If you only hava 1:1 detail-Tables in your query you could first do a selec count() on your primary key-field of your primary table. 如果您在查询中仅拥有1:1明细表,则可以首先对主表的主键字段执行selec count()。 that should only last very few milliseconds 应该只持续几毫秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM