如何在 SQL Server 中重命名名称中包含方括号的内容? [英] How to rename something in SQL Server that has square brackets in the name?
问题描述
我的一个表格中有一个列,它周围有方括号 [Book_Category]
,我想将其重命名为 Book_Category
.
I have a column in one of my tables which has square brackets around it, [Book_Category]
, which I want to rename to Book_Category
.
我尝试了以下查询:
sp_rename 'BookPublisher.[[Book_Category]]', 'Book_Category', 'COLUMN'
但我收到此错误:
消息 15253,级别 11,状态 1,过程 sp_rename,第 105 行语法解析 SQL 标识符BookPublisher.[[Book_Category]]"时出错.
Msg 15253, Level 11, State 1, Procedure sp_rename, Line 105 Syntax error parsing SQL identifier 'BookPublisher.[[Book_Category]]'.
有人可以帮我吗?
推荐答案
您可以按照与创建它相同的方式进行操作:
You do it the same way you do to create it:
exec sp_rename 'BookPublisher."[Book_Category]"', 'Book_Category', 'COLUMN';
<小时>
这是我用来测试这是否可行的一个小样本.起初我只是认为这是对如何在 SQL Server 中使用 []
的误解,结果我错了,有可能 - 你必须在括号外使用双引号.
Here's a little sample I made to test if this was even possible. At first I just assumed it was a misunderstanding of how []
can be used in SQL Server, turns out I was wrong, it is possible - you have to use double quotes to outside of the brackets.
begin tran
create table [Foo] ("[i]" int);
exec sp_help 'Foo';
exec sp_rename 'Foo."[i]"', 'i', 'column ';
exec sp_help 'Foo';
rollback tran
这篇关于如何在 SQL Server 中重命名名称中包含方括号的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!