Hi application gets an error when SSIS pkg is being executed via procedure which is activated via Service Broker queue.
Application is sending a message to a queue which activates or trigger procedure execution when msg is received in the queue.
Procedure executes ssis pkg. If we run the procedure by itself it works fine, but when its activated via the srive broker queue internally it throws error
The activated proc '[DataFlowControl].[ RunEdwB1SpotRatesExtractProc]' running on queue 'SSISDB.DataFlowControl. RECEIVE_DEST_B1_FXRATE_QUEUE' output the following: 'Cannot execute as the server principal because the principal "AC\SQL14SRVDEV_SVC" does not exist, this type of principal cannot be impersonated, or you do not have permission
AC\SQL14SRVDEV_SVC is the service account to run SQL Server services and as sysadmin role.
If we add execute as login which has sysadmin role procedure execution works fine. What's the best way to achieve what application is trying to do without security risk or providing elevated permissions.
Sample code
create queue DataFlowControl.RECEIVE_DEST_B1_FXRATE_QUEUE;
GO
ALTER QUEUE [DataFlowControl].[RECEIVE_DEST_B1_FXRATE_QUEUE]
WITH
STATUS = ON,
ACTIVATION(
STATUS = ON,
PROCEDURE_NAME = [DataFlowControl].[RunEdwExtractProc],
MAX_QUEUE_READERS = 4,
EXECUTE AS OWNER
);
GO
CREATE procedure
[DataFlowControl].[RunEdwExtractProc]
BEGIN
PRINT'[RunEdwB1SpotRatesExtractProc] started at '+format(getdate(),'yyyy-MM-dd HH:mm:ss')
DECLARE@MessageBodyVARBINARY(MAX),
@MessageTypeName
SYSNAME,
@ConversationHandle
UNIQUEIDENTIFIER;
WAITFOR(
RECEIVETOP(1)
@MessageTypeName
=message_type_name,
@MessageBody
=message_body,
@ConversationHandle
=[conversation_handle]
FROM[DataFlowControl].[RECEIVE_DEST_B1_FXRATE_QUEUE]
),TIMEOUT6000;
IF(@@ROWCOUNT>0)
BEGIN
DECLARE@execution_idBIGINT,
@ssis_execution_status
BIGINT=1;
EXECSSISDB.catalog.create_execution
@folder_name =N'EDW'
,@project_name='edw_b1_spot_rates_extract'
,@package_name='edw_b1_spot_rates_extract.dtsx'
,@execution_id=@execution_idoutput
EXECSSISDB.catalog.start_execution@execution_id
WHILE(@ssis_execution_statusNOTIN(3,4,6,7,9))
BEGIN
/*The possible values are
created (1),
running (2),
canceled (3),
failed (4),
pending (5),
ended unexpectedly (6),
succeeded (7),
stopping (8),
and completed (9)*/
WAITFORDELAY'00:00:01';--Pause for 1 second.
--Refresh the status.
SET@ssis_execution_status=(
SELECT
[executions]
.[status]
FROM
[SSISDB]
.[catalog].[executions]
WHERE
[executions]
.[execution_id]=@execution_id
);
END
END;
END
;
GO