开发者

Table/column names enclosed in square brackets, even though their names consist of legal characters

开发者 https://www.devze.com 2022-12-31 12:54 出处:网络
Square brackets allow you to use names for columns or aliases that contain characters not permitted for column names or aliases.

Square brackets allow you to use names for columns or aliases that contain characters not permitted for column names or aliases.

a) I’ve noticed that lots of times table and column names are enclosed inside square brackets, even though their names 开发者_如何学Cconsist of perfectly legal characters. Why is that?

b) As far as I know, square brackets enclosing object’s name aren’t actually a part of that name. Thus, if we create a table named [A]:

CREATE TABLE [A] ( … )

we can later reference it without using brackets:

SELECT * FROM A

But why isn’t the same true when I try to reference a column KEY from a table returned by CONTAINSTABLE function? Namely, if I omit the brackets enclosing column name, I get an error:

SELECT ct.KEY   
FROM CONTAINSTABLE(fullText,*,'some_string') as ct

thanx


KEY is a reserved word in SQL so requires the brackets to use it as a column name.

I think lots of the time you see superfluous square brackets may well be code generated by a tool. Management Studio puts them on when generating some scripts.


Brackets are not just for legal characters but to allow the use of otherwise reserved words for column names, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号