为了与SQL保留字区分开 select id from tb select [id] from tb
主要是避免关键字引用,用于区分列名,表名与关键字。另外如果列名中有空格也是要用方括号的。 例如,a表中有个列名叫from,如果你直接写select from from a肯定报错,这时就要使用方括号了。select [from] from a就可以。
if object_id('[tb]') is not null drop table [tb] go create table [tb]([TID] int,[TSID] int,[TClickCount] int) insert [tb] select 1,5,2000 union all select 2,5,1800 union all select 3,5,2100 union all select 4,3,1200 union all select 5,3,1500 union all select 6,2,1000 select tid [lihan] from tblihan ----------- 1 2 3 4 5 6(所影响的行数为 6 行)
1. 用于区别与系统有冲突鹅字眼如 order, where (看下例,我想大家都应该避免这种情况) e.g. select [order], [where] from table where [where] = 1 order by [order]2. 如字段有空格 (我也会避免这种情况,除非在 数据 Cube 环境) e.g. select [my column], [user name] from table
select id from tb
select [id] from tb
例如,a表中有个列名叫from,如果你直接写select from from a肯定报错,这时就要使用方括号了。select [from] from a就可以。
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([TID] int,[TSID] int,[TClickCount] int)
insert [tb]
select 1,5,2000 union all
select 2,5,1800 union all
select 3,5,2100 union all
select 4,3,1200 union all
select 5,3,1500 union all
select 6,2,1000
select tid [lihan] from tblihan
-----------
1
2
3
4
5
6(所影响的行数为 6 行)
e.g. select [order], [where] from table where [where] = 1 order by [order]2. 如字段有空格 (我也会避免这种情况,除非在 数据 Cube 环境)
e.g. select [my column], [user name] from table