I have the following script on an AlwaysON High Availability Group. I am trying to run a set of statements on the Primary Node only but not on the Secondary. However, the GOTO statement does not seem to work. The SQL code is read entirely and does not skip to the END if it's the Secondary node. Any ideas would be helpful. Thanks in advance.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- Use Mastergo-- T-SQL Script: Removes Current PapersDB Users Idle > 2 hours-- AlwaysOn Script Execute only on Primary Nodedeclare @debug bitset @debug = 0declare @ServerName nvarchar(256) = @@SERVERNAMEdeclare @RoleDesc nvarchar(60)select @RoleDesc = a.role_desc from sys.dm_hadr_availability_replica_states as a join sys.availability_replicas as b on b.replica_id = a.replica_id where b.replica_server_name = @@SERVERNAMEif (@debug=1) print 'Server Role: ' + @RoleDescif @RoleDesc = 'Secondary' BEGIN if (@debug=1) print 'SQL PapersDB Delete Skipped on Secondary Node' [b] goto skipDEL [/b] /* SQL DELETE on Primary Node Only */ END if @RoleDesc = 'PRIMARY' BEGIN if (@debug=1) print 'PapersDB Users Idle > 2 hours Deleted' Use PapersDB DELETE FROM CurrentUsers WHERE (iApplicationID = 10678 AND DATEDIFF(minute, dtLastTime, getDATE()) > 120) Update Statistics CurrentUsers END[b]skipDEL: [/b]
↧