Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 safe to use sp_rename?

Author  Topic 

heze
Posting Yak Master

192 Posts

Posted - 2007-06-14 : 07:50:56
hi I want to use sp_rename to rename columns only, iver researched some in internet and got a little concerned for the warnings as well as the warning you get when you run rename the "breaking script" thing, however, most of the secondary effects I read applied to renaming sprocs and views but not columns, to be "safe" i did
----------------------------------------
alter table add column
update new column with original column values
alter table drop original column
----------------------------------------
the problem with this approach is not only that I had to bundle each statement in an exec dynamic sql statement because the compiler was complaining about the "updating" of a column that does not exist, but in addition since it is a considerable number of columns I want to rename, the code is getting larger and larger when instead i could be solving my problem using one line of code
exec myDb.sp_rename 'mytable.mycolumnOriginalName','myName','column'
----------my question is: are there any counter indications for using sp_rename for this case?

thank you

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-14 : 08:10:27
What about Procs/Functions/Views using old column name? They will no longer work.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2007-06-14 : 08:14:07
h,
in this case, Im using the sp_rename in a script that creates a table, so in other words, the immediate second next step in the entire system after te table is produced, is the renamin of this field, the immediate first next step is the resaon for which Im having to rename, so no procedure, function or view is ever using the original name...
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-14 : 08:17:39
If there is nothing in between creating table and then renaming column, why don't you create table with desired column name in the first place? Why create table with wrong column name and then correct it?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-06-14 : 08:24:02
What you were doing could be a really bad idea
see
http://www.nigelrivett.net/SQLAdmin/AlterTableProblems.html

you could have done it by
alter table add column
go
update new column with original column values
go
alter table drop original column
go

to save the dynamic sql.

The issue with renaming is that sql server executes plans which use the object IDs - if the plan is not recreated then it could use an old object id - but you aren't changing any IDs here - columns are referenced via tableid + colid and the data via the offset in the data row - none of which you are changing.
To be safer you can sp_recompile the table. Even better is to restart the server.




==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2007-06-14 : 08:24:44
h,im updating 50 columns in a fact table as the intermediate step, im using a while cycle and I need a sepcific naming convention for only those 50 columns, if i name my "to tename" field using the same naming convention as those 50 fields, it will also be affected by the update and I dont want that, I need the originalname when the automatic update runs and then I need to change the name so that it follows the same convention as the 50 fields mentioned
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2007-06-14 : 08:26:18
nr,
my alter/update statements are within a stored procedure and "go" was not allowed,
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-06-14 : 08:46:06
Hold the table name, column name, logical column name in a table.
Then you can crate the table with the correct column name but use this metadata table to use the logical name in code (actually sounds like this just needs to be a flag) so the columns you don't want to perform operations on can be ignored.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2007-06-14 : 08:51:26
nr, youre right can do that, I actually have everything set up to do it that way, thanks
Go to Top of Page
   

- Advertisement -