Collate declared SQL variable

瘦欲@ 提交于 2019-12-05 04:09:02

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

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

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


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

Output:

╔══════╦══════════════════════════════╗
║ 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'.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!