views:

72

answers:

1

I need to write some code for renaming a column in SQL Server 2008.
When scripting that in Management Studio I got a double renaming :

NAME1 ==> TEMPNAME ==> NAME2

BEGIN TRANSACTION
GO
EXECUTE sp_rename N'dbo.Table_1.columFirstName', N'Tmp_columSecondName_2', 'COLUMN' 
GO
EXECUTE sp_rename N'dbo.Table_1.Tmp_columSecondName_2', N'columSecondName', 'COLUMN' 
GO
ALTER TABLE dbo.Table_1 SET (LOCK_ESCALATION = TABLE)
GO
COMMIT

But when I do it in one go, It works just fine.

Why the column first is renamed to a temporary name? Does it makes sense when coding a renaming algoritm to do the same?

Thanks!

+4  A: 

You can switch columns names between two . Management studio supports that by renaming all columns to temporary names, and then renaming them to the final names.

Blorgbeard: In the Design Window right-click menu, there is a generate change script choice.

Fredrik Staxäng
This might be the answer, but is not clear to me.
Peter
It's not? Switch A and B, you need A to TMP, B to A, TMP to B. To shift A,B,C to B,C,A you would need A to TMP, C to A, B to C, B to TMP. To shift A,B,C to C,A,B you need A to TMP, B to A, C to B, TMP to C. This gets complex, so why not just do it the way they do it.
Fredrik Staxäng
Okay, I see it now.. I didn't catch the phrase at first. Tx.
Peter
Interesting stuff - there are other places where SMSS generates more complex scripts than required in order to support more exotic scenarios, so your answer makes sense to me.
Dave Cluderay

related questions