Service-Broker

Service Broker 和包含 Execute as 的過程

  • November 19, 2015

我們在 Service Broker 上有隊列:

ALTER QUEUE [dbo].[my_queue] WITH
       STATUS = ON ,
       RETENTION = OFF ,
       ACTIVATION (  
                     STATUS = ON , 
                     PROCEDURE_NAME = [dbo].[my_detect_event] ,
                     MAX_QUEUE_READERS = 1 ,
                     EXECUTE AS N'dbo'  
                   ), 
       POISON_MESSAGE_HANDLING (STATUS = OFF)

程序有程式碼(我用RECEIVE剪切了一些文本,因為它與問題無關):

ALTER PROCEDURE dbo.my_detect_event
       AS
           BEGIN
               ......cut text with RECEIVE TOP (1) message_body FROM my_queue INTO @NotificationStore
               --EXECUTE AS LOGIN = 'user_sa'
               INSERT INTO dbo.log
                       ( time, text)
               VALUES  ( GETDATE(),
                         'test'
                         )
               ......
           END

user_sa - 擁有 SA 權限。如果服務觸發事件,則表數據插入成功。但是,如果您取消註釋 EXECUTE AS LOGIN = ‘user_sa’ 行,則會出現錯誤:

無法作為伺服器主體執行,因為主體“user_sa”不存在,無法模擬此類主體,或者您沒有權限。

請幫助找到解決方案。提前致謝!

技術資訊:Microsoft SQL Server 2012 - 11.0.5582.0 (X64) Enterprise Edition

答案在這裡: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/95f5ee43-d3b2-4a2d-bdd2-6fb0aed56458/service-broker-and-procedure-which-contains-execute-as? forum=sqlservicebroker&prof=required 簡而言之:我在目前數據庫的上下文中執行並在附近的基地進行處理。它需要從上面的連結中採取行動。

引用自:https://dba.stackexchange.com/questions/119534