I hope its OK to open a new thread in order to do new attempt from scratch instead of the previous one:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/62e72493-8e63-4ded-83bf-cdd7fd3c8261/backup-dbs-through-service-broker-succeeds-in-one-database-and-fails-in-the-other?forum=sqlservicebroker
The following code creates a system based on Service Broker in order to backup databases asynchronously:
Use master; If DB_ID('DB1') Is Not Null Drop Database DB1; If DB_ID('DB2') Is Not Null Drop Database DB2; Create Database DB1; Create Database DB2; ALTER AUTHORIZATION ON DATABASE::DB1 TO sa; ALTER AUTHORIZATION ON DATABASE::DB2 TO sa; Alter Database DB1 Set Enable_Broker; Go USE DB1 GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id=OBJECT_ID(N'[dbo].[P_BackupIni]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[P_BackupIni] GO IF EXISTS (SELECT * FROM sys.services WHERE name=N'SB_Srvc_Src_BackUp') DROP SERVICE [SB_Srvc_Src_BackUp] GO IF EXISTS (SELECT * FROM sys.services WHERE name=N'SB_Srvc_Trgt_BackUp') DROP SERVICE [SB_Srvc_Trgt_BackUp] GO IF EXISTS (SELECT * FROM sys.service_queues WHERE name=N'SB_Q_Src_BackUp') DROP QUEUE [dbo].[SB_Q_Src_BackUp] GO IF EXISTS (SELECT * FROM sys.service_queues WHERE name=N'SB_Q_Trgt_BackUp') DROP QUEUE [dbo].[SB_Q_Trgt_BackUp] IF EXISTS (SELECT * FROM sys.service_contracts WHERE name=N'SB_Cntrct_BackUp') DROP CONTRACT [SB_Cntrct_BackUp] GO IF EXISTS (SELECT * FROM sys.service_message_types WHERE name=N'SB_Msg_BackUp') DROP MESSAGE TYPE [SB_Msg_BackUp] GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id=OBJECT_ID(N'[dbo].[P_Backup]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[P_Backup] GO CREATE Procedure [dbo].[P_Backup] As Declare @DB Sysname, @Conversation_handle UniqueIdentifier, @Message_body Varchar(Max), @Message_Type_Name sysName, @I Int, @S Varchar(Max); Receive Top(1) @Conversation_handle=Conversation_handle, @Message_body=Message_body, @Message_Type_Name=Message_Type_Name From SB_Q_Trgt_BackUp; Exec sp_xml_preparedocument @I Output, @Message_body Select @DB=DB From OpenXML (@I, '/Root',1) With (DB Varchar(100)); If @DB='DB1' Backup Database DB1 To Disk='C:\Tmp\DB1_A.bak' With Compression; Else If @DB='DB2' Backup Database DB1 To Disk='C:\Tmp\DB2_A.bak' With Compression; If @DB Is Not Null Begin Select @S='Backup Database '+@DB+' To Disk=''C:\Tmp\'+@DB+'_B.bak'' With Compression;'; Exec(@S); End GO CREATE MESSAGE TYPE [SB_Msg_BackUp] AUTHORIZATION [dbo] VALIDATION=WELL_FORMED_XML GO CREATE CONTRACT [SB_Cntrct_BackUp] AUTHORIZATION [dbo] ([SB_Msg_BackUp] SENT BY ANY) GO CREATE QUEUE [dbo].[SB_Q_Src_BackUp] WITH STATUS=ON , RETENTION=OFF ON [PRIMARY] GO CREATE QUEUE [dbo].[SB_Q_Trgt_BackUp] WITH STATUS=ON , RETENTION=Off , ACTIVATION (STATUS=ON, PROCEDURE_NAME=[dbo].[P_Backup], MAX_QUEUE_READERS=1, EXECUTE AS Owner) ON [PRIMARY] GO CREATE SERVICE [SB_Srvc_Src_BackUp] AUTHORIZATION [dbo] ON QUEUE [dbo].[SB_Q_Src_BackUp] ([SB_Cntrct_BackUp]) GO CREATE SERVICE [SB_Srvc_Trgt_BackUp] AUTHORIZATION [dbo] ON QUEUE [dbo].[SB_Q_Trgt_BackUp] ([SB_Cntrct_BackUp]) GO Create Proc [dbo].[P_BackupIni] @DB Sysname As Declare @Cnvrst UniqueIdentifier; Declare @Message Varchar(Max); Select @Message='<Root DB="'+@DB+'"></Root>'; Begin Dialog @Cnvrst From Service SB_Srvc_Src_BackUp To Service N'SB_Srvc_Trgt_BackUp' On Contract SB_Cntrct_BackUp With Encryption=Off; Send On Conversation @Cnvrst Message Type SB_Msg_BackUp (@Message); Go
The user executes P_BackupIni which initiates the execution of P_Backup, and passes to it the name of the database to backup.
P_Backup procedure backups the database twice - a direct backup and a dynamic backup (=using dynamic SQL).
The system is created on DB1.
Lets try to backup DB1 & DB2:
Exec P_BackupIni @DB='DB1'; Exec P_BackupIni @DB='DB2';
Both backups of DB1 succeeded (DB1_A.bak, DB1_B.bak)
but only one backup of DB2 succeded (DB2_A.bak) and the dynamic one failed.
From the system perspective - all the backups were done by the same user/Login.
As I've already mentioned - it was tested on my laptop, I'm the only user, I'm an sa etc.
Any explanation why the dynamic backup fails?
El castellano no es mi lengua materna. Discúlpenme por los errores gramaticales, y, si pueden, corríjanme en los comentarios, o por correo electrónico. ¡Muchas gracias! Blog: http://about.me/GeriReshef