存儲過程如何選擇存儲過程中唯一的記錄
alter PROCEDURE [dbo].[Get_ProductsByCategoryId_Filter]
@CategoryId int,
@BrandId int,
@PageSize int,
@PageIndex int
AS
SET NOCOUNT ON
DECLARE @ParamDefinition AS NVARCHAR(2000)
DECLARE @sql nvarchar(4000)
SET @sql = 'select DISTINCT ProductId, Name, Description,ProductPriceId,Price,Size,Weight,ThumbnailFilename,BrandId,DeliveryId,CategoryId,HasFreeWrapping,IsPharmaceutical,BrandCategoryId,Enabled,OpenForOffer,FreeDeliveryInUK,Discontinued,EnforceStockCount,OnlyInUK,ShowPreOrderButton,MinPrice
from (SELECT P.[ProductId], P.[Name], P.[Description],PP.[ProductPriceId],PP.[Price],PP.[Size],PP.[Weight], M.[ThumbnailFilename],M.[MEDIAFILENAME], P.[BrandId], P.[DeliveryId], P.[CategoryId], P.[HasFreeWrapping], P.[IsPharmaceutical], P.[BrandCategoryId], P.[Enabled], P.[OpenForOffer], P.[FreeDeliveryInUK],
P.[Discontinued], P.[EnforceStockCount], P.[OnlyInUK], P.[ShowPreOrderButton], MIN(ISNULL(PP.[Price], 0)) AS [MinPrice],
ROW_NUMBER() OVER (Order By P.ProductId asc) AS
RowRank
FROM [Products] P WITH (NOLOCK)
LEFT JOIN [ProductPrices] PP WITH (NOLOCK) ON PP.[ProductId] = P.[ProductId]
INNER JOIN [ProductCategory] PC WITH (NOLOCK) ON P.[ProductId] = PC.[ProductId]
INNER JOIN [Categories] C WITH (NOLOCK) ON C.[CategoryId] = PC.[CategoryId]
inner join [Brands] b WITH (NOLOCK) ON P.[brandid]= b.[brandid]
inner join [ProductMedia] m with (NOLOCK) ON P.[PRODUCTID]= M.[PRODUCTID]
WHERE 1 = 1 AND P.Enabled = 1 AND P.Discontinued = 0 AND PP.Stock > 0 AND (C.[CATEGORYID][email protected] or @CategoryId=0) AND (P.[BRANDID][email protected] or @BrandId is null)
AND M.PRODUCTMEDIAID = (SELECT DISTINCT TOP 1 PRODUCTMEDIAID FROM PRODUCTMEDIA WHERE PRODUCTMEDIA.PRODUCTID= P.PRODUCTID)
GROUP BY P.[ProductId], P.[Name], P.[Description],PP.[ProductPriceId],PP.[Price],PP.[Size],PP.[Weight], M.[ThumbnailFilename],M.[MEDIAFILENAME], P.[BrandId],B.[BrandId], P.[DeliveryId], P.[CategoryId], P.[HasFreeWrapping], P.[IsPharmaceutical], P.[BrandCategoryId], P.[Enabled], P.[OpenForOffer], P.[FreeDeliveryInUK], P.[Discontinued], P.[EnforceStockCount], P.[OnlyInUK], P.[ShowPreOrderButton]
) AS ProductsWithRowNumbers
WHERE
RowRank > ' + CONVERT(nvarchar(10), @PageIndex) +
' AND RowRank <= (' + CONVERT(nvarchar(10),
@PageIndex) + ' + '
+ CONVERT(nvarchar(10), @PageSize) + ') '
SET @ParamDefinition = '@CategoryId int,@BrandId int, @PageSize int,
@PageIndex int
'
-- Execute the SQL query
EXEC sp_executesql @sql, @ParamDefinition,
@CategoryId,
@BrandId,
@PageSize ,
@PageIndex
--
GO
在上面的存儲過程的結果,我得到重複的記錄,但我不希望顯示重複的記錄,請幫助我如何我可以得到嗎
爲什麼地球上您使用動態SQL?這絕對沒有必要...... –
根據我所需要的應用要求,可以請你幫我從這個@ marc_s – user3166404
獲得獨特的記錄(沒有重複的記錄)我在產品ID上使用了獨特但仍然我得到了重複記錄,請幫我解決這個問題@marc_s – user3166404