【问题标题】:Case and COLLATE SQL_Latin1_General_CP1_CI_AS in where statementwhere 语句中的 case 和 COLLATE SQL_Latin1_General_CP1_CI_AS
【发布时间】:2017-04-27 18:35:13
【问题描述】:

我正在研究一个存储过程,它有两个输入参数,我需要使用 COLLATE SQL_Latin1_General_CP1_CI_AS 以便查询不区分大小写的结果;但是,我还想允许使用输入显示全部选项。我还没有找到一种方法让 Collat​​e 在案例陈述中工作。注意:作为一种变通方法,我将使用 4 种不同的 If 语句来编写代码,用于选择值和/或全部显示的不同场景,但如果可能的话,我想要更简洁的东西。当前代码如下

Declare 
@INCo as bCompany,
@MatBeg as bMatl,
@MatEnd as bMatl,
@Catg as bGroup,
@Model as VarChar(20),
@PatternM as VarChar(20),
@SellOn as VarChar(20),
@PatternS as VarChar(20),
@ShowZero as bYN

set @INCo = '1'
set @MatBeg = '0'
set @MatEnd = 'ZZZZZZZZZZ'
set @Catg = '0'
set @Model = '637'
set @SellOn = 'EBAY'
set @ShowZero = 'Y' 


begin
set @PatternM = '%' + @Model + '%';
set @PatternS = '%' + @SellOn + '%';

select i.INCo, i.Material, h.Description, h.Category, c.Description as CatgDesc, i.Booked as Quantity, i.PhyLoc, p.Storage, 
    i.udModelFit as FitsModel, i.udSellPriceNew as LikeNewSellPrice, i.udSellPriceUsed as UsedSellPrice, i.udSellingOn as SellingOn

from INMT i
    left outer join HQMT h on i.MatlGroup=h.MatlGroup and i.Material=h.Material
    left outer join HQMC c on h.MatlGroup=c.MatlGroup and h.Category=c.Category
    left outer join udPhysicalloc p on i.PhyLoc=p.Physicalloc

where i.INCo = (CASE when @INCo <> 0 then @INCo else i.INCo END)
    and i.Material >= @MatBeg and i.Material <= @MatEnd
    and c.Category = (CASE when @Catg <> 0 then @Catg else c.Category END)
    and i.udModelFit COLLATE SQL_Latin1_General_CP1_CI_AS like @PatternM
    and i.udSellingOn COLLATE SQL_Latin1_General_CP1_CI_AS like @PatternS
    and i.Booked >= (CASE when @ShowZero = 'N' then 1 else 0 END)
END

如果我只关心@Model 和@SellOn 的不区分大小写的结果,则此代码非常有效。但是,就像我拥有的​​其他参数一样,我想包含一些允许显示该参数的所有结果的东西。比如:

i.udModelFit = (CASE when @Model <> 'ALL' then COLLATE SQL_Latin1_General_CP1_CI_AS like @PatternM else i.udModelFit END)

例如,我想输入 @Model = 'ALL' 和 @SellOn = 'eBay' 并且结果将是任何具有 Sell On = EBAY 的模型(不区分大小写)


更新:我能够将 Where 修改为以下内容,现在我得到了想要的结果。

where i.INCo = (CASE when @INCo <> 0 then @INCo else i.INCo END)
    and i.Material >= @MatBeg and i.Material <= @MatEnd
    and c.Category = (CASE when @Catg <> 0 then @Catg else c.Category END)
    and (
        (@Model IS NULL or i.udModelFit COLLATE SQL_Latin1_General_CP1_CI_AS like @PatternM) 
        or ((i.udModelFit like '%' or i.udModelFit IS NULL) and @Model = 'ALL')
        )
    and (
        (@SellOn IS NULL or i.udSellingOn COLLATE SQL_Latin1_General_CP1_CI_AS like @PatternS)
        or ((i.udSellingOn like '%' or i.udSellingOn IS NULL) and @SellOn = 'ALL')
        )
    and i.Booked >= (CASE when @ShowZero = 'N' then 1 else 0 END)

【问题讨论】:

    标签: sql-server-2008 stored-procedures case case-sensitive collate


    【解决方案1】:

    您可以使用and( (...) or (...) ),例如

    and (@Model = 'ALL' or i.udModelFit like @PatternM SQL_Latin1_General_CP1_CI_AS)
    

    如果您想根据输入切换不同的搜索选项,您可能需要考虑使用动态 sql 编写程序,和/或添加option (recompile)

    包罗万象的查询参考:

    【讨论】:

    • 谢谢,厨房水槽和你的建议得到了它
    • @Kerry 乐于助人!
    猜你喜欢
    • 2018-12-25
    • 2014-09-08
    • 1970-01-01
    • 2013-03-23
    • 2020-03-26
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多