删除 SQL 中包含点和方括号的列

Delete column in SQL that contains dots and squared brackets

如何删除 SQL 服务器中的列名 [database].[dbo].[my_table].[col_name],即带有点和方括号的列名。换句话说,它是我想要的但以数据库名称和 shema 为前缀的列名称。 我尝试了很多基于互联网的组合,例如here 但没有成功。 谢谢。

在定界标识符中,“]”转义为“]]”、“[”和“.”。不需要转义。

像这样:

create table #tt(id int, [[database]].[dbo]].[my_table]].[col_name]]] int)

alter table #tt drop column [[database]].[dbo]].[my_table]].[col_name]]]

我不明白你是想重命名还是删除此列,但是这两种操作的方法如下

CREATE TABLE JustTest(
  Col1 INT,
  [[database]].[dbo]].[my_table]].[col_name]]] INT
);

-- To rename the column use this
EXEC sp_rename 'JustTest.[[database]].[dbo]].[my_table]].[col_name]]]', 
               'NewName', 
               'COLUMN';

-- If the table is TempTable use this
EXEC tempdb.sys.sp_rename N'#TMP.[[database]].[dbo]].[my_table]].[col_name]]]',
                N'NewName',
                N'COLUMN';

-- To drop it use this
ALTER TABLE JustTest DROP COLUMN [[database]].[dbo]].[my_table]].[col_name]]];