Which keywords are allowed to be used as names without brackets and which are not ?

Why some SQL keywords are allowed to be used as names when others are not ? Is there any pattern in determination of which I can use and which I can't without trying to compile and getting error ?

I have impression that older keywords are not allowed, but newer are allowed so that newer SQL versions are as compatible as possible with older.

In the following example rollup can be used as name without brackets, but group cannot.

if object_id('accident') is not null drop table accident
create table accident(
     state varchar(50)
    ,city varchar(50)
    ,zip varchar(50)
    ,person varchar(50)
    ,id int identity(1,1)
)

insert accident(state,city,zip,person)values
 ('NY','Manhattan',10001,'John')
,('NY','Manhattan',10001,'John')
,('NY','Manhattan',10001,'Barbara')


;with
    rollup as (
            select accident.*
                    ,lvl = grouping_id(accident.state,accident.city,accident.zip,accident.person,accident.id)
                    ,accidents=count(1)
                    ,people=0
                from accident
                group by rollup(accident.state,accident.city,accident.zip,accident.person,accident.id)
        )
        select * from rollup

;with
    [group] as (
            select accident.*
                    ,lvl = grouping_id(accident.state,accident.city,accident.zip,accident.person,accident.id)
                    ,accidents=count(1)
                    ,people=0
                from accident
                group by rollup(accident.state,accident.city,accident.zip,accident.person,accident.id)
        )
        select * from [group]

Attempt to use group as name without brackets

;with
    group as (
            select accident.*
                    ,lvl = grouping_id(accident.state,accident.city,accident.zip,accident.person,accident.id)
                    ,accidents=count(1)
                    ,people=0
                from accident
                group by rollup(accident.state,accident.city,accident.zip,accident.person,accident.id)
        )
        select * from group

gives error:

Msg 156, Level 15, State 1, Line 28
Incorrect syntax near the keyword 'group'.
Msg 156, Level 15, State 1, Line 36
Incorrect syntax near the keyword 'group'.

有帮助吗?

解决方案

There is a list of reserved keywords and you can find it in here. It's too long to paste it into this answer. All reserved keywords have to be escaped. From your example rollup is not a reserved keyword, whereas group is.

There are more kywords in ISO standards. Plase take a look at the following notes in the same link:

Additionally, the ISO standard defines a list of reserved keywords. Avoid using ISO reserved keywords for object names and identifiers. The ODBC reserved keyword list, shown in the following table, is the same as the ISO reserved keyword list.

The ISO standards reserved keywords list sometimes can be more restrictive than SQL Server and at other times less restrictive. For example, the ISO reserved keywords list contains INT. SQL Server does not have to distinguish this as a reserved keyword.

Transact-SQL reserved keywords can be used as identifiers or names of databases or database objects, such as tables, columns, views, and so on. Use either quoted identifiers or delimited identifiers. Using reserved keywords as the names of variables and stored procedure parameters is not restricted.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top