开发者

Refactor rename field in many database tables

开发者 https://www.devze.com 2023-04-07 18:13 出处:网络
I am faced with renaming a field where the same fieldname is replicated across many tables. Looking for a script or free/low cost tool which will go through all the开发者_如何学Python tables and if

I am faced with renaming a field where the same fieldname is replicated across many tables.

Looking for a script or free/low cost tool which will go through all the开发者_如何学Python tables and if fieldA exists, rename it to fieldB.

thanks


You can use SQL Server metadata tables to create dynamic sql statements for your purpose. For list of available tables you can sys.tables for list of tables and sys.columns for list of columns. using these tables you can create a table of sql statements. For executing dynamic sqls you need to sp_executesql stored procedure.

This is a sample code just to show how to use metadata tables and sp_executesql: And note that I used other metadata tables which I am more comfortable with. also you may use a cursor to run all the scripts returned by query.

CREATE Database TestDB

CREATE Table Table1 (Id int , fieldA varchar(50) )

Declare @update_query nvarchar(max)

select 
    @update_query = 'sp_rename ''' + t.TABLE_NAME + '.' + c.COLUMN_NAME + ''',''' + 'fieldB' + ''''
From INFORMATION_SCHEMA.COLUMNS c JOIN INFORMATION_SCHEMA.Tables t ON c.TABLE_CATALOG = t.TABLE_CATALOG AND c.TABLE_SCHEMA = t.TABLE_SCHEMA AND c.TABLE_NAME = t.TABLE_NAME
WHERE
    c.COLUMN_NAME = 'fieldA'

SELECT @update_query

EXEC sp_executesql @update_query


Take a look at the Database project type in VS2010, if you haven't already. It has a lot of features that make DB refactoring easier than working SQL Server Management Studio.

For example if you rename a column, it will give you build errors for all the FKs that reference the old column name. And it does a lot of build-time validation to make sure your database objects don't reference objects which no longer exist. And because all of the database objects are just kept as text files, actions like rename are pretty much just search/replace.

Also, it has very handy "sync" feature which compares the DB project scripts & databases, generates a DIFF report, and generates the scripts to move selected changes between the two (either DB project to SQL Server, or vice versa).

Having said all that, it won't automatically do the renames for you -- in other words, when you rename a column it won't fix up all references to that column throughout the project. But if you make a mistake you will get build errors when it validates the database structure. So at least makes it easy to find the places that you need to change.


If you're using Azure SQL Server, you can use Sam's answer with another input parameter to sp_rename, @objtype = 'COLUMN'.

Declare @update_query nvarchar(max)

select 
    @update_query = 'sp_rename ''' + t.TABLE_NAME + '.' + c.COLUMN_NAME + ''',''' + 'fieldB' + ''',''' + 'COLUMN' + ''''
From INFORMATION_SCHEMA.COLUMNS c JOIN INFORMATION_SCHEMA.Tables t ON c.TABLE_CATALOG = t.TABLE_CATALOG AND c.TABLE_SCHEMA = t.TABLE_SCHEMA AND c.TABLE_NAME = t.TABLE_NAME
WHERE
    c.COLUMN_NAME = 'fieldA'

SELECT @update_query

EXEC sp_executesql @update_query
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号