很明显,省略 varchar 的长度是一件坏事。不幸的是,我现在正在使用发生这种情况的代码库。广泛地。我想纠正这一点。第一步是查找出现的情况。这就是我需要帮助的地方。
使用我能想到的所有同义词在各种网络引擎上进行搜索都没有返回权威答案。我要问的是
- 我错过的其他测试用例
- 一种全面、规范的查找无长度声明的方法
通常在 Windows 开发环境(SSMS、Powershell、.Net 等)上可用的任何技术都是好的。采用更多利基技术的答案对于更广泛的社区来说会很有趣,但对我个人来说就不那么有趣了。
测试
由于所讨论的四种数据类型 - char、nchar、varchar 和 nvarchar - 都以字符 CHAR 结尾,因此我在下面的测试中单独使用它。这可以避免列表变得臃肿,并使添加更多测试变得更简单。如果需要的话,复制-粘贴-替换会很容易。
-- These are all legal; the regex must not return these
char(9)
char (9) -- with a space
char (9) -- with a tab
char (9) -- tab space tab space
char(max)
char
(9) -- a new line between type and length
character(9)
CAST(999 AS character(9))
char varying(9)
character varying(9)
CAST(999 AS char varying(9))
CAST(999 AS character varying(9))
-- These also are legal; ugly, but legal
[char](9)
[char] (9) -- with a space
[char] (9) -- with a tab
[char] (9) -- tab space tab space
[char](max)
[char]
(9) -- a new line between type and length
-- The type can also be delimited by double-quote
"char"(9)
-- All the tests using square brackets should be duplicated with other delimiters.
[character](9)
CAST(999 AS [character](9))
-- SQL Server 2022 throws an error for [character varying]
-- Msg 243, Level 16, State 1, Line 15
-- Type character varying is not a defined system type.
-- These are business terms which the regex should not return
characteristic
charge
chart
-- These are valid SQL but missing the length. These are what the search should return
char;
char ; -- a space
char ; -- a tab
char,
char ,
char = 'lorem'
cast(9 as char)
convert(char, 9)
[char];
[char] ; -- a space
[char] ; -- a tab
[char],
[char] ,
[char] = 'lorem'
cast(9 as [char])
convert([char], 9)
character
CAST(999 AS character)
char varying
character varying
CAST(999 AS char varying)
CAST(999 AS character varying)