在ORDER BY列表中遇到常量表达式

时间:2015-06-11 14:06:58

标签: sql sql-server sql-server-2008 sql-server-2012

以下是我的动态查询,但无效。

它抛出" 在ORDER BY列表中遇到一个常量表达式,位置为2。"

我不知道怎么回事。

原始动态查询:

SET @QueryVendorName = ';WITH 
cteForPriceVen AS (select AI.ItemID, AI.ItemPartNumber as ItemPartNumber, AI.ItemDescription, cteForPrice.VendorPrice as Price, cteForPrice.UpdatedDate as UpdatedDate, cteForPrice.IsLocked as IsLocked
from AerospaceItems (nolock) AI
inner join VendorItemPricing (nolock) cteForPrice
on AI.ItemPartNumber = cteForPrice.ItemPartNumber where cteForPrice.VendorName = ''' + @VendorName + ''' AND
cteForPrice.ObsoleteItem = ''' + cast (@ItemType as char(1)) + ''') select * from cteForPriceVen'

SET @OrderQuery = '
    WHERE (''' + cast (@Description as varchar(250)) + ''' = '''' OR cteForPriceVen.ItemDescription like ''%' + cast (@Description as varchar(250)) + '%'')
    AND (''' + cast (@PartNumber as varchar(99)) + ''' = '''' OR cteForPriceVen.ItemPartNumber like ''%' + cast (@PartNumber as varchar(99)) + '%'')
    AND (''' + cast (@PriceFrom as varchar(25)) + ''' = '''' OR Price >= ''' + cast (@PriceFrom as varchar(99)) + ''')
    AND (''' + cast (@PriceTo as varchar(25)) + ''' = '''' OR Price <= ''' + cast (@PriceTo as varchar(99)) + ''')
    AND (''' + cast (@DateFrom as varchar(25)) + ''' = '''' OR UpdatedDate >= ''' + cast (@DateFrom as varchar(99)) + ''')
    AND (''' + cast (@DateTo as varchar(25)) + ''' = '''' OR UpdatedDate <= ''' + cast (@DateTo as varchar(99)) + ''')

    ORDER BY
    CASE WHEN '''+ @OrderBy +'''=''ItemDescription'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''0'' THEN cteForPriceVen.ItemDescription END ASC,
    CASE WHEN '''+ @OrderBy +'''=''ItemDescription'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''1'' THEN cteForPriceVen.ItemDescription END DESC,
    CASE WHEN '''+ @OrderBy +'''=''ItemPartNumber'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''0'' THEN cteForPriceVen.ItemPartNumber END ASC,
    CASE WHEN '''+ @OrderBy +'''=''ItemPartNumber'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''1'' THEN cteForPriceVen.ItemPartNumber END DESC,
    CASE WHEN '''+ @OrderBy +'''=''Price'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''0'' THEN Price END ASC,
    CASE WHEN '''+ @OrderBy +'''=''Price'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''1'' THEN Price END DESC,
    CASE WHEN '''+ @OrderBy +'''=''UpdatedDate'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''0'' THEN UpdatedDate END ASC,
    CASE WHEN '''+ @OrderBy +'''=''UpdatedDate'' AND '''+ cast (@OrderMode as varchar(10)) +'''= ''1'' THEN UpdatedDate END DESC'

提取的查询:

;WITH 
cteForPriceVen AS (select AI.ItemID, AI.ItemPartNumber as ItemPartNumber, AI.ItemDescription, cteForPrice.VendorPrice as Price, cteForPrice.UpdatedDate as UpdatedDate, cteForPrice.IsLocked as IsLocked
from AerospaceItems (nolock) AI
inner join VendorItemPricing (nolock) cteForPrice
on AI.ItemPartNumber = cteForPrice.ItemPartNumber where cteForPrice.VendorName = 'Apple' AND
cteForPrice.ObsoleteItem = '0') select * from cteForPriceVen
    WHERE ('' = '' OR cteForPriceVen.ItemDescription like '%%')
    AND ('' = '' OR cteForPriceVen.ItemPartNumber like '%%')
    AND ('' = '' OR Price >= '')
    AND ('' = '' OR Price <= '')
    AND ('' = '' OR UpdatedDate >= '')
    AND ('' = '' OR UpdatedDate <= '')

    ORDER BY
    CASE WHEN 'ItemDescription'='ItemDescription' AND '0'= '0' THEN cteForPriceVen.ItemDescription END ASC,
    CASE WHEN 'ItemDescription'='ItemDescription' AND '0'= '1' THEN cteForPriceVen.ItemDescription END DESC

如果我逐行删除第二个订单,CASE WHEN 'ItemDescription'='ItemDescription' AND '0'= '1' THEN cteForPriceVen.ItemDescription END DESC查询似乎有效。

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:4)

第二行:

CASE WHEN 'ItemDescription'='ItemDescription' AND '0'= '1' THEN cteForPriceVen.ItemDescription END DESC

相当于NULL。您无法按NULL订购某些内容。

修改

如果此语句是由动态查询生成的,那么您需要做的是修复构建动态查询的方式:

@orderQuery = ' ORDER BY '
IF @OrderBy = 'ItemDescription'
BEGIN
  orderQuery += ' cteForPriceVen.ItemDescription '
  orderQuery += CASE WHEN @OrderMode = 1 THEN 'ASC' ELSE 'DESC' END
END

答案 1 :(得分:0)

这条线没有意义:

CASE WHEN 'ItemDescription'='ItemDescription' AND '0'= '0'

不应该是这样的:

CASE WHEN variableName = 'Value' ...