declare table_cursor cursor for select name from sys.objects where type = 'U' and name like 'AT_%'
open table_cursor fetch next from table_cursor into @table_name
while @@fetch_status = 0 begin -- replace table name, eg: 'AT_CRM_Client' -> 'CRM.Client' set @new_table_name = Replace(Replace(@table_name, 'AT_', ''), '_', '.');
-- eg:CRM -> Crm set @pre_name = LOWER(Substring(@new_table_name,0,Charindex('.',@new_table_name))); set @pre_name = STUFF(@pre_name,1,1,UPPER(SUBSTRING(@pre_name,1,1)));
--reorganize table name ge:CRM.Client -> Crm.Client set @new_table_name = STUFF(@new_table_name,1,Charindex('.',@new_table_name)-1,@pre_name);
--rename all the tables exec sp_rename @table_name,@new_table_name;
--Save the old table and new table name Insert @name_record values(@table_name,@new_table_name);
fetch next from table_cursor into @table_name end
close table_cursor deallocate table_cursor
--select * from @name_record;
------------------------------ --modify all the views ------------------------------- declare @view_name VARCHAR(100); declare @view_text VARCHAR(MAX);
declare view_cursor cursor for select top 1 name from sys.views
open view_cursor fetch next from view_cursor into @view_name
while @@FETCH_STATUS = 0 begin set @view_text = (select top 1 text from syscomments where id = OBJECT_id(@view_name)); ----------------------------------------------------------------- --replace view_text declare name_record_cursor cursor for select * from @name_record; open name_record_cursor; declare @old_name VARCHAR(100); declare @new_name VARCHAR(100); fetch next from name_record_cursor into @old_name,@new_name; while @@FETCH_STATUS = 0 begin set @view_text = Replace(@view_text, 'dbo.'+@old_name, '[dbo].['+@new_name+'] ') --print @old_name + @new_name; fetch next from name_record_cursor into @old_name,@new_name; end close name_record_cursor deallocate name_record_cursor --------------------------------------------- --replace CREATE to ALTER set @view_text = Replace(@view_text,'CREATE','ALTER'); --print @view_text; exec @view_text; fetch next from view_cursor into @view_name end