Recently we were having issues on our database server and after long efforts it was decided to change the database server. So we managed to restore the database on another server, change the connection string, etc. Everything was going as planned until we tried to access the website from a web browser.
We started getting errors about database objects not being found. Later we found out that it occured as a result of the modified schema name. Since there are hundreds of database objects (tables, views and stored procedures) in a Kentico database, it is not feasible to change all of them manually, one-by-one. Is there a practical way of doing this?
You may use following script by just copy/paste for all objects
NOTE: You need to Change schema names in script !
Thanks for the tip.. Here is my update to same, where I added a crlf to output as well as put brackets around the SchemaName and ObjectName, because some of the objects had a '-' in the name and the brackets solved that naming error.
Yes, it is possible.
To change the schema of a database object you need to run the following SQL script:
Where ObjectName can be the name of a table, a view or a stored procedure. The problem seems to be getting the list of all database objects with a given shcema name. Thankfully, there is a system table named sys.Objects that stores all database objects. The following query will generate all needed SQL scripts to complete this task:
Where type 'U' denotes user tables, 'V' denotes views and 'P' denotes stored procedures.
Running the above script will generate the SQL commands needed to transfer objects from one schema to another. Something like this:
Now you can run all these generated queries to complete the transfer operation.
Here's the SQL I ran, to move all tables in my database (spread across several schemas) into the "dbo" schema:
Phew!