在sql server中使用带有DISTINCT的动态ORDER BY

时间:2014-06-25 12:47:57

标签: sql sql-server

DECLARE @OrderBy VARCHAR(100)

SET @OrderBy='DealerCode ASC'

SELECT DISTINCT TOP (10)    
    d.Id, d.DealerCode,d.AffiliationCode,d.LegalName,d.ShipperCode,d.PrimaryUserId,d.PrimaryContactId,d.Zip,              
    d.Address1,d.Address2,d.IsActive,u.IsActive,d.City,d.[State],c.Name,u.UserName,d.RegistrationIntiatedDate,
    d.RegistrationCompletedDate,              
    rs.[Description],u.EntityId,dbo.fnGetCheckHours(d.Id)               
    FROM Dealer d              
    LEFT JOIN [User] u ON d.PrimaryUserId=u.Id              
    LEFT JOIN RegistrationSource rs ON d.RegistrationSourceId=rs.Id              
    LEFT JOIN Country c ON d.CountryId=c.id              
    LEFT JOIN ShippingNote sn ON d.Id=sn.DestinationId              
    LEFT JOIN Yard y ON sn.YardID=y.Code              
    LEFT JOIN Terminal t ON y.TerminalId=t.Id                
    WHERE t.Id='9a77168f-e5b6-4dd0-b26e-f95249c8e189' AND sn.destinationtype='Dealer'
    ORDER BY +'d.'+@OrderBy

但我收到以下错误:

  

Msg 145,Level 15,State 1,Line 4   如果指定了SELECT DISTINCT,则ORDER BY项必须出现在选择列表中。

我也在使用CASE,但我又遇到了错误

2 个答案:

答案 0 :(得分:1)

尝试使用动态查询

DECLARE @OrderBy VARCHAR(100)
DECLARE @SQL VARCHAR(8000)

SET @OrderBy='DealerCode ASC'

SET @SQL = 'SELECT DISTINCT TOP (10)    
    d.Id, d.DealerCode,d.AffiliationCode,d.LegalName,d.ShipperCode,d.PrimaryUserId,d.PrimaryContactId,d.Zip,              
    d.Address1,d.Address2,d.IsActive,u.IsActive,d.City,d.[State],c.Name,u.UserName,d.RegistrationIntiatedDate,
    d.RegistrationCompletedDate,              
    rs.[Description],u.EntityId,dbo.fnGetCheckHours(d.Id)               
    FROM Dealer d              
    LEFT JOIN [User] u ON d.PrimaryUserId=u.Id              
    LEFT JOIN RegistrationSource rs ON d.RegistrationSourceId=rs.Id              
    LEFT JOIN Country c ON d.CountryId=c.id              
    LEFT JOIN ShippingNote sn ON d.Id=sn.DestinationId              
    LEFT JOIN Yard y ON sn.YardID=y.Code              
    LEFT JOIN Terminal t ON y.TerminalId=t.Id                
    WHERE t.Id=''9a77168f-e5b6-4dd0-b26e-f95249c8e189'' AND sn.destinationtype=''Dealer''
    ORDER BY d.' + @OrderBy

    EXEC(@SQL)

答案 1 :(得分:0)

您无法在编译的查询中将顺序设置为变量。您可以尝试像这样构建查询

DECLARE @OrderBy VARCHAR(100)

SET @OrderBy='DealerCode'

SELECT DISTINCT TOP (10)    
    d.Id, d.DealerCode,d.AffiliationCode,d.LegalName,d.ShipperCode,d.PrimaryUserId,d.PrimaryContactId,d.Zip,              
    d.Address1,d.Address2,d.IsActive,u.IsActive,d.City,d.[State],c.Name,u.UserName,d.RegistrationIntiatedDate,
    d.RegistrationCompletedDate,              
    rs.[Description],u.EntityId,dbo.fnGetCheckHours(d.Id)               
    FROM Dealer d              
    LEFT JOIN [User] u ON d.PrimaryUserId=u.Id              
    LEFT JOIN RegistrationSource rs ON d.RegistrationSourceId=rs.Id              
    LEFT JOIN Country c ON d.CountryId=c.id              
    LEFT JOIN ShippingNote sn ON d.Id=sn.DestinationId              
    LEFT JOIN Yard y ON sn.YardID=y.Code              
    LEFT JOIN Terminal t ON y.TerminalId=t.Id                
    WHERE t.Id='9a77168f-e5b6-4dd0-b26e-f95249c8e189' AND sn.destinationtype='Dealer'
    ORDER BY  CASE @OrderBy WHEN  'DealerCode' THEN d.DealerCode
                            WHEN  'AffiliationCode' THEN d.AffiliationCode
              END