从存储过程返回唯一行

时间:2016-10-06 15:06:33

标签: sql-server stored-procedures sql-server-2012

这是我的程序,这会返回多行,因为功能ID有一个供应商ID的多个ID如何修改它以便它返回唯一的行,请帮助我。

ALTER PROCEDURE GetVendorsPageWise
 @PageIndex INT = 1
      ,@PageSize INT = 10
      ,@type nvarchar(max)
      ,@city nvarchar(max)
      ,@query nvarchar(max)
      ,@RecordCount INT OUTPUT
      AS
BEGIN
      SET NOCOUNT ON;
SELECT ROW_NUMBER() OVER
      (
            order by Vendor_PrimaryInfo.Vendor_ID asc
      )AS RowNumber
      ,Unit_Table.Unit_title, Vendor_Base_Price.Base_Price, Vendor_Base_Price.showprice, Category_Table.Title, Vendor_Registration.Business_Name, 
                         Vendor_PrimaryInfo.Street_Address, Vendor_PrimaryInfo.Locality, Vendor_PrimaryInfo.Nearest_Landmark, Vendor_PrimaryInfo.City, Vendor_PrimaryInfo.State, 
                         Vendor_PrimaryInfo.Country, Vendor_PrimaryInfo.PostalCode, Vendor_PrimaryInfo.Latitude, Vendor_PrimaryInfo.Longitude, Vendor_PrimaryInfo.ImageUrl, 
                         Vendor_PrimaryInfo.ContactNo, Vendor_PrimaryInfo.Email,Vendor_PrimaryInfo.Vendor_ID,Vendor_Value_Table.Feature_ID,Vendor_Value_Table.Value_Text

     INTO #Results
      FROM  Unit_Table INNER JOIN
                         Vendor_Base_Price ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID INNER JOIN
                         Vendor_PrimaryInfo ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID INNER JOIN
                         Vendor_Registration ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID AND 
                         Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID INNER JOIN
                         Category_Table ON Vendor_Registration.Category_ID = Category_Table.Category_ID
                         LEFT JOIN
                         Vendor_Value_Table ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID LEFT JOIN
                         Feature_Table ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
                         where Vendor_Registration.Category_ID=@type and Vendor_PrimaryInfo.City=@city + @query
                          order by Vendor_PrimaryInfo.Vendor_ID


      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
      END

This is result set with duplicate items

1 个答案:

答案 0 :(得分:0)

您将使用DISTINCT关键字

SELECT DISTINCT * 
  FROM #Results
WHERE 
  RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 
  AND
  (((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1