如何计算存储过程的结果并仅显示100行

时间:2017-05-10 05:58:27

标签: sql sql-server stored-procedures count

我编写了一个存储过程来搜索数据库中的用户。但有时它会返回太多用户,而且需要花费太多时间。

因此,如果结果集超过500行,我决定只返回前500行。但我不知道如何从存储过程中获取计数。

请帮帮我。

我的原始存储过程如下。感谢

ALTER PROCEDURE [dbo].[up_Select_SearchUser]
    @companyCode    varchar(50)
,   @keyword        nvarchar(20)
,   @isRelative     bit
,   @langCode       varchar(10)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT     
        LOWER(M.UserID) AS EmpID,
        LOWER(M.EmpID) AS REmpID,
        dbo.uf_CompanyPhone(M.CompanyPhone, M.ExtensionNumber) AS ExtensionNumber,  
        M.CellPhone,
        dbo.uf_TeamManagerYN(M.UserID) AS TeamChiefYN, 
        ISNULL(M.DisplayName, '') AS DisplayName, 
        ISNULL(M.DisplayName_Eng, '') AS DisplayName_Eng,
        M.EMail, 
        M.MainDeptCode AS DeptCode,
        ISNULL(DT.DisplayName, '') AS DeptName,
        ISNULL(CL.CompanyCode, '') AS CompanyCode,
        ISNULL(CL.CompanyName, '') AS CompanyName,
        ISNULL(R.RankCode, '') AS RankCode,
        ISNULL(R.RankName, '') AS RankName, 
        ISNULL(J.JobCode, '') AS JobCode,
        ISNULL(J.JobName, '') AS JobName,
        ISNULL(D.DutyCode, '') AS DutyCode,
        ISNULL(D.DutyName, '') AS DutyName,
        ISNULL(M.FaxNumber, '') AS FaxNumber,
        ISNULL(M.ADDisplayName, '') AS ADDisplayName,
        '' AS Address,
        ISNULL(M.CompanyPhone, '') AS CompanyPhone, 
        ISNULL(R.SortOrder, '') AS RankOrder
    FROM     
        dbo.tb_User M WITH (NOLOCK) 
    INNER JOIN 
        dbo.tb_Dept DT WITH (NOLOCK) ON M.MainDeptCode = DT.DeptCode 
    INNER JOIN 
        dbo.tb_Rank R WITH (NOLOCK) ON M.RankCode = R.RankCode
                                    AND M.GroupCode = R.GroupCode
                                    AND M.DetailCode = R.DetailCode
                                    AND M.CompanyCode = R.CompanyCode
    INNER JOIN 
        dbo.tb_Company CL WITH (NOLOCK) ON M.CompanyCode = CL.CompanyCode
    LEFT OUTER JOIN 
        dbo.tb_Duty D WITH (NOLOCK) ON M.DutyCode = D.DutyCode 
                                    AND M.GroupCode = D.GroupCode
                                    AND M.DetailCode = D.DetailCode
                                    AND M.CompanyCode = D.CompanyCode
    LEFT OUTER JOIN 
        dbo.tb_Job J WITH (NOLOCK) ON M.JobCode = J.JobCode 
    WHERE    
        (M.DisplayName LIKE '%' + @keyword + '%' 
         OR M.UserID LIKE '%' + @keyword + '%') 
        AND M.DisplayYN = 'Y' 
        AND M.CompanyCode LIKE @companyCode
    ORDER BY 
        RankOrder, JobCode, DisplayName
END

6 个答案:

答案 0 :(得分:0)

Select @Count = COUNT() 
From Table 
Where <Your Condition>

然后使用IF声明:

if @Count > 500 then 
    SELECT TOP 500 
        LOWER(M.UserID) = AS EmpID 

答案 1 :(得分:0)

TOP 500添加到select查询中。这将确保结果计数不会超过500.如果它小于500,则需要记录的数量。如果它大于500,它将过滤前500个记录。

ALTER PROCEDURE [dbo].[up_Select_SearchUser]
    @companyCode    varchar(50)
,   @keyword        nvarchar(20)
,   @isRelative     bit
,   @langCode       varchar(10)
AS
BEGIN
    SET NOCOUNT ON;

SELECT  TOP 500   LOWER(M.UserID)                                            AS EmpID 
         , LOWER(M.EmpID)                                             AS REmpID 
         , dbo.uf_CompanyPhone(M.CompanyPhone, M.ExtensionNumber)     AS ExtensionNumber 
         , M.CellPhone 
         , dbo.uf_TeamManagerYN(M.UserID)                             AS TeamChiefYN 
         , ISNULL(M.DisplayName,'')                                   AS DisplayName 
         , ISNULL(M.DisplayName_Eng,'')                               AS DisplayName_Eng 
         , M.EMail 
         , M.MainDeptCode                                             AS DeptCode 
         , ISNULL(DT.DisplayName,'')                                  AS DeptName 
         , ISNULL(CL.CompanyCode,'')                                  AS CompanyCode 
         , ISNULL(CL.CompanyName,'')                                  AS CompanyName 
         , ISNULL(R.RankCode,'')                                      AS RankCode 
         , ISNULL(R.RankName,'')                                      AS RankName 
         , ISNULL(J.JobCode,'')                                       AS JobCode 
         , ISNULL(J.JobName,'')                                       AS JobName 
         , ISNULL(D.DutyCode,'')                                      AS DutyCode 
         , ISNULL(D.DutyName,'')                                      AS DutyName 
         , ISNULL(M.FaxNumber,'')                                     AS FaxNumber 
         , ISNULL(M.ADDisplayName,'')                                 AS ADDisplayName 
         , ''                                                         AS Address
         , ISNULL(M.CompanyPhone, '')                                 AS CompanyPhone
         , ISNULL(R.SortOrder,'')                                     AS RankOrder
FROM     dbo.tb_User M WITH (NOLOCK) 
         INNER JOIN dbo.tb_Dept DT WITH (NOLOCK) 
            ON M.MainDeptCode = DT.DeptCode 
         INNER JOIN dbo.tb_Rank R WITH (NOLOCK) 
            ON M.RankCode = R.RankCode
            AND M.GroupCode = R.GroupCode
            AND M.DetailCode = R.DetailCode
            AND M.CompanyCode = R.CompanyCode
         INNER JOIN dbo.tb_Company CL WITH (NOLOCK) 
            ON M.CompanyCode = CL.CompanyCode             
         LEFT OUTER JOIN dbo.tb_Duty D WITH (NOLOCK) 
            ON M.DutyCode = D.DutyCode 
            AND M.GroupCode = D.GroupCode
            AND M.DetailCode = D.DetailCode
            AND M.CompanyCode = D.CompanyCode
         LEFT OUTER JOIN dbo.tb_Job J WITH (NOLOCK) 
            ON M.JobCode = J.JobCode 
WHERE    (M.DisplayName LIKE '%' + @keyword + '%' 
           OR M.UserID LIKE '%' + @keyword + '%') 
         AND M.DisplayYN = 'Y' 
         AND M.CompanyCode LIKE @companyCode
ORDER BY RankOrder , JobCode, DisplayName

答案 2 :(得分:0)

使用此语句而不是SELECT TOP:

SELECT * FROM Products
WHERE id BETWEEN 10 AND 20;

以获得更好的控制

更好的例子wiev: Equivalent of LIMIT and OFFSET for SQL Server?

答案 3 :(得分:0)

据我所知,你的问题是你想要只选择500或100条记录,直到你想要选择多少条记录,你可以使用

select top 500 [lisofyourcolums] from your table;

如果您想要进行分页,您只需在存储过程中进行更改,如下所示

ALTER PROCEDURE [dbo].[up_Select_SearchUser]
    @companyCode    varchar(50)
,   @keyword        nvarchar(20)
,   @isRelative     bit
,   @langCode       varchar(10)
,   @startRowIndex int=1
,   @maximumRows int=500
AS
BEGIN
SET NOCOUNT ON;
      DECLARE  @startRow int,@totalRows int
    SET @startRowIndex =  (@startRowIndex - 1)  * @maximumRows
    Print @startRowIndex

    IF @startRowIndex = 0 
     SET @startRowIndex = 1
    Else
     SET @startRowIndex = @startRowIndex+1
    Print @startRowIndex
    SET ROWCOUNT @startRowIndex  


    select [yourcolumnList] from yourtable 

    SET ROWCOUNT @maximumRows 


SELECT  

END

我希望这会对你有所帮助......

答案 4 :(得分:0)

试试这个

ALTER PROCEDURE [dbo].[up_Select_SearchUser]
        @companyCode    varchar(50)
    ,   @keyword        nvarchar(20)
    ,   @isRelative     bit
    ,   @langCode       varchar(10)
    ,   @PageNumber INT = 1--Give Page number you required 
    ,   @PageSize   INT = 100 --Give number of rows you required per Page
    AS
    BEGIN
        SET NOCOUNT ON;


    SELECT     LOWER(M.UserID)                                            AS EmpID 
             , LOWER(M.EmpID)                                             AS REmpID 
             , dbo.uf_CompanyPhone(M.CompanyPhone, M.ExtensionNumber)     AS ExtensionNumber 
             , M.CellPhone 
             , dbo.uf_TeamManagerYN(M.UserID)                             AS TeamChiefYN 
             , ISNULL(M.DisplayName,'')                                   AS DisplayName 
             , ISNULL(M.DisplayName_Eng,'')                               AS DisplayName_Eng 
             , M.EMail 
             , M.MainDeptCode                                             AS DeptCode 
             , ISNULL(DT.DisplayName,'')                                  AS DeptName 
             , ISNULL(CL.CompanyCode,'')                                  AS CompanyCode 
             , ISNULL(CL.CompanyName,'')                                  AS CompanyName 
             , ISNULL(R.RankCode,'')                                      AS RankCode 
             , ISNULL(R.RankName,'')                                      AS RankName 
             , ISNULL(J.JobCode,'')                                       AS JobCode 
             , ISNULL(J.JobName,'')                                       AS JobName 
             , ISNULL(D.DutyCode,'')                                      AS DutyCode 
             , ISNULL(D.DutyName,'')                                      AS DutyName 
             , ISNULL(M.FaxNumber,'')                                     AS FaxNumber 
             , ISNULL(M.ADDisplayName,'')                                 AS ADDisplayName 
             , ''                                                         AS Address
             , ISNULL(M.CompanyPhone, '')                                 AS CompanyPhone
             , ISNULL(R.SortOrder,'')                                     AS RankOrder
    FROM     dbo.tb_User M WITH (NOLOCK) 
             INNER JOIN dbo.tb_Dept DT WITH (NOLOCK) 
                ON M.MainDeptCode = DT.DeptCode 
             INNER JOIN dbo.tb_Rank R WITH (NOLOCK) 
                ON M.RankCode = R.RankCode
                AND M.GroupCode = R.GroupCode
                AND M.DetailCode = R.DetailCode
                AND M.CompanyCode = R.CompanyCode
             INNER JOIN dbo.tb_Company CL WITH (NOLOCK) 
                ON M.CompanyCode = CL.CompanyCode             
             LEFT OUTER JOIN dbo.tb_Duty D WITH (NOLOCK) 
                ON M.DutyCode = D.DutyCode 
                AND M.GroupCode = D.GroupCode
                AND M.DetailCode = D.DetailCode
                AND M.CompanyCode = D.CompanyCode
             LEFT OUTER JOIN dbo.tb_Job J WITH (NOLOCK) 
                ON M.JobCode = J.JobCode 
    WHERE    (M.DisplayName LIKE '%' + @keyword + '%' 
               OR M.UserID LIKE '%' + @keyword + '%') 
             AND M.DisplayYN = 'Y' 
             AND M.CompanyCode LIKE @companyCode
    ORDER BY RankOrder , JobCode, DisplayName

        OFFSET @PageSize * (@PageNumber - 1) ROWS
        FETCH NEXT @PageSize ROWS ONLY 


    END

答案 5 :(得分:0)

声明一个变量以从用户获取输入以显示他想要返回的记录数

ALTER PROCEDURE [dbo].[up_Select_SearchUser]
    @companyCode    varchar(50)
,   @keyword        nvarchar(20)
,   @isRelative     bit
,   @langCode       varchar(10)
,   @NumberOfRows INT
AS
BEGIN
    SET NOCOUNT ON;

SELECT TOP @NumberOfRows 
    LOWER(M.UserID) AS EmpID,
    LOWER(M.EmpID) AS REmpID,
    dbo.uf_CompanyPhone(M.CompanyPhone, M.ExtensionNumber) AS ExtensionNumber,  
    M.CellPhone,
    dbo.uf_TeamManagerYN(M.UserID) AS TeamChiefYN, 
    ISNULL(M.DisplayName, '') AS DisplayName, 
    ISNULL(M.DisplayName_Eng, '') AS DisplayName_Eng,
    M.EMail, 
    M.MainDeptCode AS DeptCode,
    ISNULL(DT.DisplayName, '') AS DeptName,
    ISNULL(CL.CompanyCode, '') AS CompanyCode,
    ISNULL(CL.CompanyName, '') AS CompanyName,
    ISNULL(R.RankCode, '') AS RankCode,
    ISNULL(R.RankName, '') AS RankName, 
    ISNULL(J.JobCode, '') AS JobCode,
    ISNULL(J.JobName, '') AS JobName,
    ISNULL(D.DutyCode, '') AS DutyCode,
    ISNULL(D.DutyName, '') AS DutyName,
    ISNULL(M.FaxNumber, '') AS FaxNumber,
    ISNULL(M.ADDisplayName, '') AS ADDisplayName,
    '' AS Address,
    ISNULL(M.CompanyPhone, '') AS CompanyPhone, 
    ISNULL(R.SortOrder, '') AS RankOrder
FROM     
    dbo.tb_User M WITH (NOLOCK) 
INNER JOIN 
    dbo.tb_Dept DT WITH (NOLOCK) ON M.MainDeptCode = DT.DeptCode 
INNER JOIN 
    dbo.tb_Rank R WITH (NOLOCK) ON M.RankCode = R.RankCode
                                AND M.GroupCode = R.GroupCode
                                AND M.DetailCode = R.DetailCode
                                AND M.CompanyCode = R.CompanyCode
INNER JOIN 
    dbo.tb_Company CL WITH (NOLOCK) ON M.CompanyCode = CL.CompanyCode
LEFT OUTER JOIN 
    dbo.tb_Duty D WITH (NOLOCK) ON M.DutyCode = D.DutyCode 
                                AND M.GroupCode = D.GroupCode
                                AND M.DetailCode = D.DetailCode
                                AND M.CompanyCode = D.CompanyCode
LEFT OUTER JOIN 
    dbo.tb_Job J WITH (NOLOCK) ON M.JobCode = J.JobCode 
WHERE    
    (M.DisplayName LIKE '%' + @keyword + '%' 
     OR M.UserID LIKE '%' + @keyword + '%') 
    AND M.DisplayYN = 'Y' 
    AND M.CompanyCode LIKE @companyCode
ORDER BY 
    RankOrder, JobCode, DisplayName
END