整理声明的 SQL 变量

Collate declared SQL variable(整理声明的 SQL 变量)
本文介绍了整理声明的 SQL 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在查看这段代码复制如下,它寻找非 ASCII 字符......

I've been looking at this code, reproduced below, that looks for non-ASCII characters...

select line,
  patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line) as [Position],
  substring(Line, patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line), 1) as [InvalidCharacter],
  ascii(substring(line, patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line), 1)) as [ASCIICode]
from staging.APARMRE1
where patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line) > 0

我突然想到我想为 '%[^ !-~]%' COLLATE Latin1_General_BIN 声明一个变量,而不是每次都写出来,但是

and it just strikes me that I'd want to declare a variable for '%[^ !-~]%' COLLATE Latin1_General_BIN instead of writing it out every time, but

declare @regex varchar(20) = '%[^ !-~]%' COLLATE Latin1_General_BIN;

select line,
  patindex(@regex, Line) as [Position],
  substring(Line, patindex(@regex, Line), 1) as [InvalidCharacter],
  ascii(substring(line, patindex(@regex, Line), 1)) as [ASCIICode]
from staging.APARMRE1
where patindex(@regex, Line) > 0

只是不做同样的事情.我只是缺少一些语法吗?不可能吗?

just doesn't do the same thing. Am I just missing some syntax? Is it impossible?

推荐答案

这是正常的.创建变量时,它采用数据库的默认排序规则.

It is normal. When you create a variable it takes default collation for database.

DECLARE @regex varchar(20) = '%[^ !-~]%' COLLATE Latin1_General_BIN;

带有 COLLATE Latin1_General_BIN 的字符串被隐式转换为带有数据库默认排序规则的字符串.

Your string with COLLATE Latin1_General_BIN is implicitly casted to string with your database default collation.

<小时>例如数据库是Case-Insensitive.我使用您的语法创建区分大小写的语法并检查它的元数据:


For example database is Case-Insensitive. I use your syntax to create case-sensitive one and check metadata of it:

DECLARE @v1 varchar(100) = 'ABC' COLLATE Latin1_General_CS_AS;

SELECT name, collation_name
FROM sys.dm_exec_describe_first_result_set(
    N'SELECT @v1 AS [@v1]', N'@v1 varchar(100)', 0);

LiveDemo

输出:

╔══════╦══════════════════════════════╗
║ name ║        collation_name        ║
╠══════╬══════════════════════════════╣
║ @v1  ║ SQL_Latin1_General_CP1_CI_AS ║
╚══════╩══════════════════════════════╝

变量(不包括表变量中的列)不允许定义排序规则,因此没有如下语法:

Variables(excluding columns in table variables) do not allow to define collation so there is no syntax like:

DECLARE @v1 varchar(100) COLLATE Latin1_General_CS_AS = 'ABC' ;
-- Incorrect syntax near the keyword 'COLLATE'.

这篇关于整理声明的 SQL 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)