views:

257

answers:

4

While renaming the column name, the square bracket is included in the column name, which I think is a bug, Here is a sample code snippet,

 create table [TestTable]

(TestColumnName nvarchar(30))

select TestColumnName from TestTable

sp_rename '[TestTable].[TestColumnName]', '[RenamedColumnName]', 'Column'

select [RenamedColumnName] from TestTable -- does not work "Invalid column name 'RenamedColumnName'."

select RenamedColumnName from TestTable -- does not work "Invalid column name 'RenamedColumnName'."

select * from [TestTable]  -- works fine!!!

The bug here is that the column rename includes the square brackets, I found this which says that the "first character must be one of the following", but "[" does not seem be included in the list, is there a problem with sp_rename or sql server itself?, as it allows alteration of column name to start with a square bracket.

A: 

the square brackets are used to mark the boundaries of the columns names. That way you can include reserved words, spaces, single quotes etc. in the column names and the script will not fail.

sp_rename: http://msdn.microsoft.com/en-us/library/aa238878(SQL.80).aspx

from BOL: This example renames the contact title column in the customers table to title: EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'

try this:

sp_rename 'TestTable.TestColumnName', 'RenamedColumnName', 'Column'

since you are passing in strings into the procedure you don't need the square braces "[","]"

you can use "[","]" in the first parameter, but if you use them in the second parameter, they become part of the actual column name:

create table [TestTable2]([Test ColumnName] nvarchar(30))
exec sp_help testtable2
exec sp_rename 'dbo.TestTable2.[Test ColumnName]', 'Renamed ColumnName', 'Column'
exec sp_help testtable2
KM
It is sort of confusing, then why does this throw an error "select [RenamedColumnName] from TestTable "
81967
@KM - without quotes, you get a syntax error
AdaTheDev
+1  A: 

Data error !!!

Its not

sp_rename '[TestTable].[TestColumnName]', '[RenamedColumnName]', 'Column'

It should be like this

sp_rename '[TestTable].[TestColumnName]', 'RenamedColumnName', 'Column'

then

select [RenamedColumnName] from TestTable -- works fine!!!

select RenamedColumnName from TestTable -- works fine!!!

select * from [TestTable]  -- works fine!!!

Even though the new column name is with space like "Renamed ColumnName" NO NEED TO use the square brackets in the

solairaja
I know it is a dataerror from the code end solairaja, but why in the first place does sql server allow the first character to be NON-identifier in a table or column name? is it a bug?
81967
see actually, the function sp_rename will not do any validation on the data what ur supplying on the parameter 2. It merely send the data what ur sending to inside the square brackets. so when u supply along with the square brackets its considering tat as the name again. the best proof is the last line in my answer even with space u can send the data.
solairaja
It's not a bug - you're allowed to use non-standard field names, so long as you enclose them in square brackets. If you use legal names, you don't need the brackets.
Robin Bennett
+2  A: 

The column in your code has been renamed to one that actually includes [] - to query this column you'll have to use

SELECT [[RenamedColumnName]]] FROM TestTable

] is a delimited identifier, so you have to escape it. For ], this means an additional ] for each one used in the name.

CodeByMoonlight
surprisingly for me, your query works, as it has two sqaure brackets first and 3 at the end,,, :-S
81967
+1  A: 

It's not a bug, as "[" and "]" are valid characters within a column name. sp_rename has to work by receiving the exact column name you want to use - after all how would it know whether you wanted a column actually called "[MyColumnWithBrackets]" or "MyColumnWithBrackets". Hence, if you provide a name, it's treated literally and does not require you to manually enclose (e.g.) column names with spaces in, in brackets

AdaTheDev
thanks for the reply AdaTheDev, but on the other hand, can you create a table legally by having a square brackets starting the its name?
81967
You can, by using the same [[]]] syntax as in my response.
CodeByMoonlight
Of course, you really wouldn't want to do this. Using any special characters in object names will probably come back to haunt you.
CodeByMoonlight
Thanks for answering CodeByMoonLight
81967
Yep, I agree with CodeByMoonlight - I'm personally not a fan of using anything other than alphanumeric and underscore characters in object names. Keep it simple, remove the risk of being bitten.
AdaTheDev
definitely, keep it simple, but some strange situations makes them more interesting, isn't it :)
81967