Dear Mark;
I have worked on the previous stored procedure, and edit it for you and this new SP can help you out.
create procedure All_Col_rename
as
begin
Declare @varOldcol varchar(100)
Declare @varNewcol Varchar(100)
declare @varTablename varchar(40), @strSQL varchar(150)
print @varOldcol
print @varNewcol
declare tab_col_cur cursor
for
select COLUMN_NAME , TABLE_NAME from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like 'B%'
open tab_col_cur
fetch next from tab_col_cur into @varOldcol , @varTablename
while @@fetch_status = 0
begin
-- it will replace first 'B' with ''
SET @varNewcol = Stuff(@varOldcol , 1 , 1, '')
set @strSQL= 'exec sp_rename ' + '''' + @varTablename + '.' + @varOldcol + ''''+ ', ' + ''''+@varNewcol +''''+ ' , ' + '''' + 'COLUMN' + ''''
exec (@strSQL)
fetch next from tab_col_cur into @varOldcol , @varTablename
end
close tab_col_cur
deallocate tab_col_cur
end
But, dear Mark you when you will run this SP it will give you following Caution "Caution: Changing any part of an object name could break scripts and stored procedures."
and confirmation of those columns which are renamed :
The COLUMN was renamed to 'rimaryID'.
and Error Message for those which are referenece and dependencies.
Server: Msg 15336, Level 16, State 1, Procedure sp_rename, Line 368
Object '[column name]' cannot be renamed because the object participates in enforced dependencies.
Plesae , be carefull when running these kind of scripts b/c they will change your so many columns.
Please, take backup then test this SP on test database.
Regards,
Muhammad Essa Mughal