sql server - drop and create a synonym while using select * sys.synonyms with a case statement -


i using following code drop , create synonyms in database.

select * sys.synonyms  drop synonym database1.btxaddr; drop synonym database1.btxsupb; create synonym database1.btxaddr   database2.da2.btxaddr;   create synonym database1.btxsupb  datebase2.da3.btxsupb;  truncate table database2.da2.btxaddr; truncate table database2.da3.btxsupb; insert database2.da3.btxsupb select * database2.da2.btxsupb; 

the problem having have differnt folders want synonym point to. folders da1, da2, da3. synonym da pointing @ da1. want have if statement looks pointing change pointer da2 , da3 , da1 when need change them.

i have read lot of posts on here , can change synonyms myself. save me time in long run need change it.

this need , makes easeier.

declare @synname varchar(100), @syntarget varchar(150)  declare syns cursor select  base_object_name   sys.synonyms    name in ('btxaddr', 'btxsupb')  open syns fetch next syns @synname, @syntarget while @@fetch_status <> -1 begin  print 'drop synonym ' + @synname  set @syntarget = case                     when @syntarget '%[da2]%' replace(@syntarget,'[da2]','[da3]')                    when @syntarget '%[da3]%' replace(@syntarget,'[da3]','[da1]')                    when @syntarget '%[da1]%' replace(@syntarget,'[da1]','[da2]')               end  print 'create synonym ' + @synname + ' ' +@syntarget  fetch next syns @synname, @syntarget end close syns deallocate syns 

i'm posting came incase else has same issue wont have go through trouble have gone through.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -