Let me say at least one time in this article…..”Backup your Database before running the created text file.” Ok, now that I have said that let’s talk about a problem that occurs a lot, especially if you are working in multiple databases. If you have databases for Production and Test and you clone regularly then you know that sometimes you have to update stored procedures and views if they are linked to other production and test databases.
For example. Suppose you have four databases. Two production and Two Test. In your production databases you have one for all of your HR information while the other is your Payroll information. So you have something like the chart below. (see Figure 1)
Now when you clone your production HR database to test your links for the new test instance are pointing to the production instance of Payroll (Figure 2). This means that you have to open up the newly cloned test database and point the views and procedures to the test HR database see below. Sometimes this can be very time consuming. I developed this script to help me with this issue.
Use [TestDatabase] Go Declare @CurDatabaseName varchar(40) Declare @NewDatabaseName varchar(40) Declare @sql varchar(3000) Set @CurDatabaseName = 'NameOfDatabaseToLookFor' --Look for this in the Views/Procedures Set @NewDatabaseName = 'NameofDatabasetoReplaceWith' --Replace it with this Value DECLARE @sObjectName varchar(80) DECLARE @sObjectType varchar(80) DECLARE @sDefinition varchar(MAX) DECLARE @sModifiedDef varchar(MAX) --Create Temp Table CREATE TABLE ##tbl ( ObjectName varchar(80) NOT NULL, ObjectType varchar(80) NOT NULL, ObjCurDefin varchar(max) Not NULL, ObjNewDefin varchar(max) not Null ) Insert Into ##tbl SELECT b.Name AS [ObjectName], CASE WHEN b.type ='p' THEN 'Stored Procedure' WHEN b.type ='v' THEN 'View' ELSE b.TYPE END AS [ObjectType] ,a.definition AS [Definition] ,Replace(Replace((REPLACE(definition,@CurDatabaseName,@NewDataBaseName)), 'CREATE ','ALTER '),'''','''') AS [ModifiedDefinition] FROM sys.sql_modules a JOIN ( select type, name,object_id from sys.objects where type in ( 'p' -- procedures ,'v' --views ) and is_ms_shipped = 0 )b ON a.object_id=b.object_id INNER JOIN ( SELECT Name, 'p' as Type FROM sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE '%' + @CurDatabaseName + '%' Union All sELECT NAME, 'v' as Type FROM SYS.VIEWS WHERE OBJECT_DEFINITION(object_id) LIKE '%' + @CurDatabaseName + '%' ) fd on b.Name = fd.Name and b.type=fd.type order by b.name Select * from ##tbl select @sql = 'bcp "' + 'select ' + 'ObjNewDefin + CHAR(13)+CHAR(10) + ''GO'' as action ' + ' from ##tbl ' + 'order by ObjectName" queryout C:\New_Query.txt -c -T ' exec master..xp_cmdshell @sql DROP TABLE ##tbl
The net result of this process is that it will first return all views and stored procedures where it finds the @CurDatabaseName. Once they are found then the @CurDatabaseName is replaced with @NewDatabaseName. Once this is done then it creates a file called New_Query and adds all of these script changes into one file. Upon completion of this then you can open up the script in a query window and run it in the Test instance.
Hope you find this helpful and remember to always backup your database before running this. Running this in the test environment has very limited downsize risk. If it fails you just reclone production.
Looking for quality web hosting? Look no further than Arvixe Web Hosting!