There is an example dataBase in which we have some tables.
The word "Account" is included in the names of 5 tables in that database.
For example we have:
Table1: FactAccount
Table2: FactAccountBalance
Table3: DimAccountTime
"Account" is also included in the name of some of the columns.
We need a stored procedure to replace the word "Account" with "Items" in both column and table names, so that at the end of the stored procedure we have:
Table1: FactItems
Table2: FactItemsBalance
Table3: DimItemsTime
I have tried a combination of sp_rename
and the replace
function but I was not successful.
Best Answer
This solution uses the sys.sp_rename() database engine stored procedure.
But read carefully the warning message:
You can get table and column names by querying the
INFORMATION_SCHEMA
system information schema views (but see The case againstINFORMATION_SCHEMA
views by Aaron Bertrand).I have set up the next schema on Rextester:
Rename columns
Rextester here
Rename tables
Rextester here
Now, if you query
INFORMATION_SCHEMA
again, replacingaccount
withitems
, this is the result: