I understand that square brackets allow the use of reserved names or previously disallowed characters such as spaces in your identifiers. I thought adding them everywhere was good practice. (See What is the use of the square brackets [] in sql statements?)
我理解方括号允许使用保留名称或以前不允许的字符,例如标识符中的空格。我认为在各处添加它们都是很好的做法。 (请参阅sql语句中方括号[]的用法是什么?)
However, I notice that when I use them in the COL_LENGTH function, I get some unexpected results:
但是,我注意到当我在COL_LENGTH函数中使用它们时,我得到了一些意想不到的结果:
SELECT COL_LENGTH(N'[TestTable]', N'[RatingID]') -- Returns NULL
SELECT COL_LENGTH(N'TestTable', N'[RatingID]') -- Returns NULL
SELECT COL_LENGTH(N'[TestTable]', N'RatingID') -- Returns 10
SELECT COL_LENGTH(N'TestTable', N'RatingID') -- Returns 10
I can see that by defining the column name in single quotes, the square brackets become redundant, but I don't understand why they break the call. That square brackets work for the table argument increases my confusion.
我可以看到,通过用单引号定义列名,方括号变得多余,但我不明白为什么它们会中断调用。表格参数的方括号工作增加了我的困惑。
Is there a rule for when square brackets shouldn't be used?
不应该使用方括号的规则吗?
1 个解决方案
#1
2
Do not pass square brackets into functions that take strings only use in actual SQL statements. The function is looking for a table with the name including the square brackets.
不要将方括号传递给仅在实际SQL语句中使用字符串的函数。该函数正在查找名称包括方括号的表。
#1
2
Do not pass square brackets into functions that take strings only use in actual SQL statements. The function is looking for a table with the name including the square brackets.
不要将方括号传递给仅在实际SQL语句中使用字符串的函数。该函数正在查找名称包括方括号的表。