Malyadri
Malyadri

Reputation: 3

Need to change Databasename in views dynamically in SQL Server

We are migrating the server and the time of migration we are changing existing databasename to new name. my problem is I have created multiple views and Procs in that database tables. Now I need to replace/modifi in all view/Procs with new database name. Is there any possibility to change or replace existing dn name to new db name in all views/procs in dynamiclly?

Upvotes: 0

Views: 742

Answers (1)

Hamed Naeemaei
Hamed Naeemaei

Reputation: 9638

You can run this query, after get result run NewModifiedQuery column as a query

SELECT a.definition OldCreationQuery
    ,Replace ((REPLACE(definition,'OldDatabaseName','NewDatabaseName')),'Create','ALTER') NewModifiedQuery
FROM sys.sql_modules a

 where a.definition like '%OldDatabaseName%' 

Upvotes: 3

Related Questions