Quantcast
Channel: SQL Server High Availability and Disaster Recovery forum
Viewing all articles
Browse latest Browse all 4532

SQL AG script adding DB's Automatically

$
0
0

Hi All:

We are in the process of creating our own scripts, when ever the application creates new database, the DB will be add to Alwayson group and its working fine for us.

Today, we are planning to add one more check.

For some-reason, if the secondary replica having same database - we want to delete it.

Is it possible to delete it from Link server?

Link server already created with @notename in below script.

When we try to delete manually also, not working using link server.

Drop database [linkservername]..[test1234]

=====================================
Error:
Msg 102, Level 15, State 1, Line 43
Incorrect syntax near '.'
==================================

This is what we are trying to with hard luck, any suggestions.

     declare @avagroupname  nvarchar(128)='',
             @tsql_restore  nvarchar(1000)='',
             @tsql_hadr     nvarchar(255)='',
             @debug int,
             @dbname nvarchar(255)='';
begin

set @dbname = 'test1234';
set  @debug = 1;
set @avagroupname='AG2016'

-- if there is a same database on the secondary node , will be deleted first
        -- get the note list
       declare @notename nvarchar(128)
       select  top 1 @notename= dh.replica_server_name from  sys.availability_replicas dh join sys.dm_hadr_availability_replica_states dap
       on dh.group_id= dap.group_id and dh.replica_id=dap.replica_id join sys.availability_groups ag
       on ag.group_id=dap.group_id where role=2 and ag.name =@avagroupname  order by dh.replica_server_name desc
       print @notename
       --end
        --while @@ROWCOUNT>0 --and @notename  is not null
          --  begin
            print 'test';
                 set @tsql_restore=N'
                     exec (''if exists (select * from sys.databases where name='''''+@notename+'..'+@dbname+N''''')
                     drop database '+@notename+'..'+@dbname+N'';
                    
                    
                 if @debug>0  print @tsql_restore
                   exec (@tsql_restore)
                    
                     ---
                      
                    
                 select  top 1 @notename= dh.replica_server_name from  sys.availability_replicas dh join sys.dm_hadr_availability_replica_states dap
                  on dh.group_id= dap.group_id and dh.replica_id=dap.replica_id join sys.availability_groups ag
                  on ag.group_id=dap.group_id where role=2 and ag.name =@avagroupname and dh.replica_server_name<@notename   order by dh.replica_server_name desc
            --end

end
=================

Thanks,
Satish Kumar


Thanks, Satish Kumar. Please mark as this post as answered if my anser helps you to resolves your issue :)


Viewing all articles
Browse latest Browse all 4532

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>