Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 在where语句中对SQL拉丁语通用语句CP1 CI语句进行大小写和对比_Sql Server 2008_Stored Procedures_Case_Case Sensitive_Collate - Fatal编程技术网

Sql server 2008 在where语句中对SQL拉丁语通用语句CP1 CI语句进行大小写和对比

Sql server 2008 在where语句中对SQL拉丁语通用语句CP1 CI语句进行大小写和对比,sql-server-2008,stored-procedures,case,case-sensitive,collate,Sql Server 2008,Stored Procedures,Case,Case Sensitive,Collate,我正在处理一个存储过程,它有两个输入参数,我需要使用COLLATE SQL\u Latin1\u General\u CP1\u CI\u来查询不区分大小写的结果;然而,我还想考虑一个带有输入的ShowAll选项。我还没有找到一种方法,使校对在案例陈述中起作用。注意:作为一种解决方法,我将针对选择值和/或全部显示的不同场景使用4种不同的If语句进行编码,但如果可能,我希望使用更干净的语句。下面是当前代码 Declare @INCo as bCompany, @MatBeg as bMatl,

我正在处理一个存储过程,它有两个输入参数,我需要使用COLLATE SQL\u Latin1\u General\u CP1\u CI\u来查询不区分大小写的结果;然而,我还想考虑一个带有输入的ShowAll选项。我还没有找到一种方法,使校对在案例陈述中起作用。注意:作为一种解决方法,我将针对选择值和/或全部显示的不同场景使用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
声明
@INCo作为B公司,
@MatBeg作为bMatl,
@MatEnd作为bMatl,
@Catg作为B组,
@模型为VarChar(20),
@PatternM为VarChar(20),
@SellOn as VarChar(20),
@模式为VarChar(20),
@显示零为bYN
设置@INCo='1'
set@MatBeg='0'
set@MatEnd='zzzzzz'
设置@Catg='0'
设置@Model='637'
set@SellOn='EBAY'
设置@ShowZero='Y'
开始
设置@PatternM='%'+@Model+'%';
设置@PatternS='%'+@SellOn+'%';
选择i.INCo、i.Material、h.描述、h.类别、c.描述作为CatgDesc、i.预订作为数量、i.PhyLoc、p.存储、,
i、 udModelFit作为FitsModel,i.UDSellPrice作为LikeWellPrice新建,i.UDSellPrice作为UsedSellPrice,i.UDSellInOn作为SellingOn
来自INMT i
i.MatlGroup=h.MatlGroup和i.Material=h.Material上的左外连接HQMT h
h.MatlGroup=c.MatlGroup和h.Category=c.Category上的左外联接HQMC
i.PhyLoc=p.PhysicalOC上的左外连接UDPHysicalOC p
式中i.INCo=(当@INCo 0时为@INCo else i.INCo END时为案例)
和i.Material>=@MatBeg和i.Material=(当@ShowZero='N'时为1,否则为0结束)
结束
如果我只关心@Model和@SellOn的结果不区分大小写,那么这段代码就非常有用。但是,与我拥有的其他参数一样,我希望包括一些允许显示该参数的所有结果的内容。比如:

i.udModelFit = (CASE when @Model <> 'ALL' then COLLATE SQL_Latin1_General_CP1_CI_AS like @PatternM else i.udModelFit END)
i.udModelFit=(当@Model'ALL'然后像@PatternM else i.udModelFit END一样整理SQL\u Latin1\u General\u CP1\u CI\u时的情况)
因此,作为一个例子,我想输入@Model='ALL'和@SellOn='eBay',结果将是Sell On=eBay的任何模型(不区分大小写)


更新:我能够修改以下内容的位置,现在我得到了想要的结果

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)
其中i.INCo=(当@INCo 0然后@INCo else i.INCo END时的情况)
和i.Material>=@MatBeg和i.Material=(当@ShowZero='N'时为1,否则为0结束)
您可以使用
和(…)或(…)
例如

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

如果您想根据输入切换不同的搜索选项,您可能需要考虑使用动态SQL编写程序,和/或添加<代码>选项(重新编译)< /> >。p> 捕获所有查询引用:


谢谢,厨房的水槽和你的建议都收到了it@Kerry很乐意帮忙!