我想找到一个表中的所有行,并匹配一个确切区分大小写的字符串.不幸的是,我的表具有不区分大小写的排序规则.
执行此操作的最有效方法是什么.
例如.
我希望以下内容不返回任何行:
select * from sysobject where name = 'Sysbinobjs'
Run Code Online (Sandbox Code Playgroud)
对于答案,假设@match在一个变量中:
declare @match varchar(4000)
set @match = 'sysbinobjs'
Run Code Online (Sandbox Code Playgroud)
编辑
澄清,确保尾随空格被正确处理,我想要一个完全匹配,考虑到尾随空格,所以'Hello'只会匹配'Hello'而不是'Hello'
这是一段有效的代码片段,以安全有效的方式执行此操作非常棘手.双重匹配是为了避免表扫描(查看执行计划).varbinary铸造强制长度和大小写匹配.因此,尾随空间得到妥善处理,套管得到妥善处理.
请注意,要比较的类型需要与表中的类型相同才能使varbinary转换工作(例如,sysobject中的名称是nvarchar,因此@match将需要是nvarchar).
declare @match nvarchar(4000)
set @match = 'sysbinobjs'
select * from sysobjects
where name = @match and
cast(name as varbinary(4000)) = cast(@match as varbinary(4000))
Run Code Online (Sandbox Code Playgroud)
实际上我不明白为什么你必须首先进行转义,这是通过COLLATE语句内置到SQL服务器中.
select * from sysobjects
WHERE name = @match and --Get all relevant hits from the index before doing the final case sensitive test
name COLLATE Latin1_General_CS_AS = @match COLLATE Latin1_General_CS_AS
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12231 次 |
| 最近记录: |