gpt4 book ai didi

sql - 整理声明的 SQL 变量

转载 作者:行者123 更新时间:2023-12-02 11:20:28 36 4
gpt4 key购买 nike

我一直在看this code,复制如下,查找非 ASCII 字符...

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 声明一个变量,而不是每次都写出来,但是

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

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

最佳答案

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

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

使用 COLLATE Latin1_General_BIN 的字符串将隐式转换为具有数据库默认排序规则的字符串。

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

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 ║
╚══════╩══════════════════════════════╝

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

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

关于sql - 整理声明的 SQL 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34096720/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com