How to Rename Column SQL Server with []

zqry0prt  于 2023-06-21  发布在  SQL Server
关注(0)|答案(1)|浏览(169)

enter image description here

I entered the wrong column name and included [] as shown above [avail_memory_mb], I want to remove [] using the syntax, how do I do that?

I have tried using syntax EXEC sp_RENAME '[db_isdv_itsm].[dbo].[tbl_m_comp_sysboot_time].[avail_memory_mb]', 'avail_memory', 'COLUMN'

but an error appears

Msg 15248, Level 11, State 1, Procedure sp_RENAME, Line 247 Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.

bqf10yzr

bqf10yzr1#

The simplest method, in this case, would be to use a different delimit identifier to brackets ( [] ) for your column's name:

EXEC sys.sp_rename N'dbo.tbl_m_comp_sysboot_time."[avail_memory_mb]"', N'avail_memory', N'COLUMN';

If you "must" use brackets, then you need to escape the right bracket ( ] ):

EXEC sys.sp_rename N'dbo.tbl_m_comp_sysboot_time.[[avail_memory_mb]]]', N'avail_memory', N'COLUMN';

db<>fiddle

Note that I also removed the database name in both examples. This is because sys.sp_rename doesn't support 4 part naming. Connect to the database first, and then use the system procedure.

If you ever need to know what an object's literal should be, you can use QUOTENAME to find it out as well. For example:

SELECT QUOTENAME(N'[avail_memory_mb]');

Which brings back the nvarchar(258) value [[avail_memory_mb]]] . So, if you really wanted, you could do something like:

DECLARE @ObjectPath nvarchar(1000) = CONCAT(QUOTENAME(N'dbo'),N'.',QUOTENAME(N'tbl_m_comp_sysboot_time'),'.',QUOTENAME(N'[avail_memory_mb]'));
EXEC sys.sp_rename @ObjectPath, N'avail_memory', N'COLUMN';

相关问题