SQL SPROC返回一些重复记录

时间:2011-11-16 16:11:48

标签: sql-server stored-procedures

我认为这会导致笛卡尔效应,但结果总是在返回的数据中返回两个相同的产品。

我如何强制选择明显的?我尝试修改最后的select语句以包含“Select Distinct Top ...”但它给了我一个关于语法的错误。很抱歉,如果这是一个简单的问题,我的SQL技能就不足了。

(
@ProductSKUs varchar(500),
@CategoryIDs varchar(40),
@RecordCount int
)
AS
BEGIN
declare @Products table (ProductSKU varchar(8), ProductID int not null, TrackInventoryBySizeAndColor int not null)
declare @TempCID table(CategoryID int not null default 0)
declare @CountCID int
set @CountCID=0
declare @productIDcount int
select @productIDcount = count(*) from dbo.Split(@ProductSKUs, ',')
declare @categoryIDcount int
select @categoryIDcount = @RecordCount - @productIDcount

-- product SKUs first
insert into @Products(ProductSKU, ProductID, TrackInventoryBySizeAndColor)
select p.SKU, p.ProductID, p.TrackInventoryBySizeAndColor
from dbo.Product p with (nolock)
join dbo.Split(@ProductSKUs, ',') pi on p.SKU = pi.items
-- now variant SKUs
insert into @Products(ProductSKU, ProductID, TrackInventoryBySizeAndColor)
select pv.SKUSuffix, pv.ProductID, ISNULL(pv.TrackInventoryBySizeAndColor, 0)
from dbo.ProductVariant pv with (nolock)
join dbo.Split(@ProductSKUs, ',') pi on pv.SKUSuffix = pi.items
--debug
--SELECT * FROM @Products
if   @categoryIDcount > 0 begin
 insert into @TempCID(CategoryID)
  select c.CategoryID  from Category c with(nolock) --get the subcats
  join dbo.Split(@CategoryIDs, ',') ci on c.ParentCategoryID = cast(ci.items as int)
    union
  select c2.CategoryID  from Category c2 with(nolock)  --get the category itself
  join dbo.Split(@CategoryIDs, ',') ci on c2.CategoryID = cast(ci.items as int)
insert into @TempCID(CategoryID)
  select c.CategoryID from Category c with(nolock) 
  join @TempCID tc on c.ParentCategoryID = tc.CategoryID --get level 2 subcats    
  set @CountCID = @@ROWCOUNT
    insert into @Products(ProductSKU, ProductID, TrackInventoryBySizeAndColor)
        select TOP (@categoryIDcount) p.SKU, p.ProductID, p.TrackInventoryBySizeAndColor
        from dbo.Product p with (nolock)
        left join dbo.ProductCategory pc with (nolock) on p.ProductID = pc.ProductID 
        where pc.CategoryID in (select tc.CategoryID from @TempCID tc)
        AND pc.ProductID Not in (SELECT ProductID FROM @Products)
    ORDER BY CHECKSUM(NEWID())
end
--debug
--SELECT * FROM @Products
select top(@RecordCount)
    p.ProductID,
    p.Name,
    pv.VariantID,
    pv.Name as VariantName,
    p.ProductGUID,
    p.Summary,
    p.Description,
    p.SEKeywords,
    p.SEDescription,
    p.SpecTitle,
    p.MiscText,
    p.SwatchImageMap,
    p.IsFeaturedTeaser,
    p.FroogleDescription,
    p.SETitle,
    p.SENoScript,
    p.SEAltText,
    p.SizeOptionPrompt,
    p.ColorOptionPrompt,
    p.TextOptionPrompt,
    p.ProductTypeID,
    p.TaxClassID,
    p.SKU,
    p.ManufacturerPartNumber,
    p.SalesPromptID,
    p.SpecCall,
    p.SpecsInline,
    p.IsFeatured,
    p.XmlPackage,
    p.ColWidth,
    p.Published,
    p.RequiresRegistration,
    p.Looks,
    p.Notes,
    p.QuantityDiscountID,
    p.RelatedProducts,
    p.UpsellProducts,
    p.UpsellProductDiscountPercentage,
    p.RelatedDocuments,
    p.TrackInventoryBySizeAndColor,
    p.TrackInventoryBySize,
    p.TrackInventoryByColor,
    p.IsAKit,
    p.ShowInProductBrowser,
    p.IsAPack,
    p.PackSize,
    p.ShowBuyButton,
    p.RequiresProducts,
    p.HidePriceUntilCart,
    p.IsCalltoOrder,
    p.ExcludeFromPriceFeeds,
    p.RequiresTextOption,
    p.TextOptionMaxLength,
    p.SEName,
    p.Deleted,
    p.CreatedOn,
    p.ImageFileNameOverride,
    pv.VariantGUID,
    pv.Description as VariantDescription,
    pv.SEKeywords as VariantSEKeywords,
    pv.SEDescription as VariantSEDescription,
    pv.Colors,
    pv.ColorSKUModifiers,
    pv.Sizes,
    pv.SizeSKUModifiers,
    pv.FroogleDescription as VariantFroogleDescription,
    pv.SKUSuffix,
    pv.ManufacturerPartNumber as VariantManufacturerPartNumber,
    pv.Price,
    pv.CustomerEntersPrice, 
    pv.CustomerEntersPricePrompt,
    isnull(pv.SalePrice, 0) SalePrice,
    cast(isnull(pv.Weight,0) as decimal(10,1)) Weight,
    pv.MSRP,
    pv.Cost,
    isnull(pv.Points,0) Points,
    pv.Dimensions,
    pv.DisplayOrder as VariantDisplayOrder,
    pv.Notes as VariantNotes,
    pv.IsTaxable,
    pv.IsShipSeparately,
    pv.FreeShipping,
    pv.IsDownload,
    pv.DownloadLocation,
    pv.Published as VariantPublished,
    pv.IsSecureAttachment,
    pv.IsRecurring,
    pv.RecurringInterval,
    pv.RecurringIntervalType,
    pv.SubscriptionInterval,
    pv.SEName as VariantSEName,
    pv.RestrictedQuantities,
    pv.MinimumQuantity,
    pv.Deleted as VariantDeleted,
    pv.CreatedOn as VariantCreatedOn,
    d.Name as DistributorName,
    d.DistributorID,
    d.SEName as DistributorSEName,
    m.ManufacturerID,
    m.Name as ManufacturerName,
    m.SEName as ManufacturerSEName,
    s.Name as SalesPromptName
from dbo.Product p with (nolock)
    left join dbo.ProductVariant pv with (nolock) on p.ProductID = pv.ProductID
    join @Products pid on p.SKU = pid.ProductSKU OR pv.SKUSuffix = pid.ProductSKU
    left join dbo.SalesPrompt s with (nolock) on p.SalesPromptID = s.SalesPromptID 
    left join dbo.ProductManufacturer pm with (nolock) on p.ProductID = pm.ProductID 
    left join dbo.Manufacturer m with (nolock) on pm.ManufacturerID = m.ManufacturerID 
    left join dbo.ProductDistributor pd with (nolock) on p.ProductID = pd.ProductID
    left join dbo.Distributor d with (nolock) on pd.DistributorID = d.DistributorID
    where p.Deleted = 0 
    and p.Published = 1
    ORDER BY p.ShowBuyButton desc

编辑:@RecordCount是最终select语句中要返回的记录数:     选择顶部(@RecordCount) - 将其传递给SPROC。

1 个答案:

答案 0 :(得分:0)

更新2 : 我的不好..我不知道你可以使用top这样看到更新的代码以获得正确的语法 -

如果你需要做一个top distinct,你需要它 -

select distinct top (@RecordCount) p.ProductID, p.Name, pv.VariantID ..等等

我的确建议您使用group by并根据需要重写查询。您不应该为那么多列使用distinct。