DECLARE @preferredReplica INT
SET @preferredReplica = (SELECT
[master].sys.Fn_hadr_backup_is_preferred_replica('TJ_MES'))
IF ( @preferredReplica = 1 )
BEGIN
DECLARE @FileName varchar(1000)
SELECT @FileName = (SELECT 'D:\DBBackup\Files\TJ_MES' + convert(varchar(500), GetDate(),112) + '.bak')
BACKUP DATABASE TJ_MES TO DISK=@FileName
SELECT @FileName = (SELECT 'D:\DBBackup\Files\U4DMMES' + convert(varchar(500), GetDate(),112) + '.bak')
BACKUP DATABASE U4DMMES TO DISK=@FileName
END