|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article explains how to automatically rename every relation in your database. BackgroundThe idea (and the underlying algorithm) is simple: Take all the relations in the database, look at the tables involved in the relation and give each one the name " With previous versions of SQL Server, it was easier because the user could directly update (with a single statement) system catalogues, but in SQL Server 2005 this feature was disabled for consistency reasons. In SQL Server 2005, there are a lot of useful views lying over the system catalogues that let the user know about everything in every database. The code uses those views to accomplish the task. Using the CodeThe code is just a T-SQL block of code, so you can:
Points of InterestThis code makes use of some new SQL Server 2005 features. To make the code simpler, it was divided logically using Common table expressions (CTE). In Depth LookThe logic is simple: obtain a list of actual foreign keys on a DB and rename them using the First of all, we need to obtain every foreign key present in our database. Next, we should translate object IDs into object names. This CTE stores:
The third step is to translate the information obtained in the second step to a more useful thing: Old relation name and New relation name. The fourth step is used to take into consideration (for the rename process) only the relation names that don't already exist (because maybe someone has already fixed some of them manually or they were created with the right name). Any hints/comments are welcome.. and if you find this article useful, don't forget to rate it. :) HistoryI always hated history.. I prefer what is still to come.. :)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||