日期:2014-05-17  浏览次数:20673 次

sql语句查处多条重复数据
ALTER PROCEDURE [dbo].[up_CharacteristicValue_SelectNew]
@ProductGroupCode varchar(20),
@CharacteristicCode varchar(20),
@CharacteristicValueCode varchar(40),
@BomCode varchar(20),
@MachineFamilyID int,
@Name nvarchar(40),
@Status int
AS
BEGIN

if @BomCode <> 'Characteristic'
begin
select distinct LTrim(RTrim(a.ProductGroupCode))+LTrim(RTrim(a.BOMCode))+LTrim(RTrim(a.CharacteristicCode))+LTrim(RTrim(a.CharacteristicValueCode)) as ID, a.*
from (select f.Name as CharacteristicName,e.* from CharacteristicValue e left join Characteristic f on e.ProductGroupCode = f.ProductGroupCode and e.CharacteristicCode = f.CharacteristicCode) a
where a.ProductGroupCode=@ProductGroupCode
and (@Status = -1 or a.Status= @Status)
and (@BomCode = '' or a.BOMCode= @BomCode)
and (@CharacteristicCode = '' or a.CharacteristicCode= @CharacteristicCode)
and (@CharacteristicValueCode = '' or a.CharacteristicValueCode like '%' + @CharacteristicValueCode + '%')
and (@Name = '' or a.Name like '%' + @Name + '%')
and (@MachineFamilyID = -1 or (a.BOMCode in (select BOMCode from ConfigurableBOM where MachineFamilyID=@MachineFamilyID)))
  order by a.ProductGroupCode,a.BOMCode,a.CharacteristicCode,a.sequence
end 
else
begin
select distinct LTrim(RTrim(e.ProductGroupCode))+LTrim(RTrim(e.CharacteristicCode))+LTrim(RTrim(e.CharacteristicValueCode)) as ID, 
f.Name as CharacteristicName, 
e.AgentPrice, e.BOCostPrice, e.CharacteristicCode, e.CharacteristicValueCode, e.Description,
e.IsCustomizeSolution, e.IsExtendService, e.IsFinishedProduct, e.IsPresale, e.Name,
e.PLM_Code, e.PLM_Quantity, e.PresaleDate, e.PriceID, e.ProductGroupCode, e.R3EName, e.R3Name,
e.Sequence, e.Status, e.ViewStatus 
from CharacteristicValue e left join Characteristic f on e.ProductGroupCode = f.ProductGroupCode and e.CharacteristicCode = f.CharacteristicCode
where e.ProductGroupCode=@ProductGroupCode
--取可卖的和不可卖的
--and e.Status = 0 --(@Status = -1 or e.Status= @Status)
and (@CharacteristicCode = '' or e.CharacteristicCode= @CharacteristicCode)
and (@CharacteristicValueCode = '' or e.CharacteristicValueCode like '%' + @CharacteristicValueCode + '%')
and (@Name = '' or e.Name like '%' + @Name + '%')
  order by e.ProductGroupCode,e.CharacteristicCode,e.sequence
end

END

麻烦各位帮忙看看这段存储过程有什么问题吗?总是查出来很多重复数据

------解决方案--------------------
你的关联字段里面的值是唯一的吗
------解决方案--------------------
长篇的~~~