DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''DB_BASE''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [DB_BASE] TO DISK = N''H:\Backup\DB_BASE\DB_BASE_backup_2023_10_11_235731_1142855.bak'' WITH NOFORMAT, NOINIT, NAME = N''DB_BASE_backup_2023_10_11_235731_1142855'', SKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''DB_BASE''))
IF (@preferredReplica = 1)
BEGIN
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N''DB_BASE'' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N''DB_BASE'' )
if @backupSetId is null begin raiserror(N''Verify failed. Backup information for database ''''DB_BASE'''' not found.'', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N''H:\Backup\DB_BASE\DB_BASE_backup_2023_10_11_235731_1142855.bak'' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
END