sql server - Is there a way to replace code and update a view using a tsql script -


i have 20 views in database reference database, let's call database a. need way update these views script point different database, database b. there way replace name of database in view name of database b using single tsql script , keep views intact? can replace , output views query window want execute alter statements generated , not have run output manually.

update similar this: https://stackoverflow.com/a/2983927/6084613 have output executed script. possible?

you need put query definition variable , execute sp_executesql.

but: i'd cautious using referenced script without more work. example, not views have exact text 'create view'. if hand written , might ' create view' or 'create view' example. also, 'db1' might me written '[db1]'.

so either add lot more sophisticated logic, or validated eye before exectuting.

   declare @querydef nvarchar(max)     select @querydef = replace (replace (sm.definition, 'create view', 'alter view'), 'db1.', 'db2.')     sys.sql_modules sm join sys.objects o          on sm.object_id = o.object_id             sm.definition '%db1.%' , o.type = 'v'      print @querydef     exec sp_executesql @querydef 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -