'DIALOG' 和 'CONVERSATION' 命令可以使用变量吗?

Is it possible to use variables with the 'DIALOG' and 'CONVERSATION' commands?

我已经能够设置我的 Visual Studio (2019) 数据库项目并使用适合表、存储过程、视图等的数据库引用。它们似乎都与我的模式匹配得很好'进口。但是,在引用同一数据库的合同、类型、服务等时,我似乎无法避免错误。当我引用目标数据库的服务时,我没有问题(我认为这是因为它接受基本的 NVARCHAR 字符串而不是强类型架构。

我得到了以下示例,它只是将消息发送到另一个数据库的服务。我收到错误:"SQL71502: Procedure: [dbo].[z_Queue_SendMessage] has an unresolved reference to object [@VariableName]",@VariableName 为以下所有内容:(@RequestMessageType, @BasicContract, @InitiatorService)

CREATE PROCEDURE [dbo].[z_Queue_SendMessage]
    @ProcedureName VARCHAR(1000) = 'Procedure', 
    @SubProcedureName VARCHAR(500) = 'SO', 
    @ProcedureType VARCHAR(200) = 'Delete', 
    @Inserted AS XML = NULL, 
    @Deleted AS XML = NULL
AS


DECLARE @InitDlgHandle UNIQUEIDENTIFIER;

DECLARE @RequestMsg NVARCHAR(MAX);
DECLARE @Message XML;

DECLARE @RequestMessageType NVARCHAR(100) = '//$(WebDb)' + N'/Queue/RequestMessage'
DECLARE @InitiatorService NVARCHAR(100) = '//$(WebDb)' + N'/Queue/InitiatorService'
DECLARE @BasicContract NVARCHAR(100) = '//$(WebDb)' + N'/Queue/BasicContract'
DECLARE @TargetService NVARCHAR(100) = '//$(WebDb)' + N'/Queue/TargetService'


BEGIN TRANSACTION;

BEGIN DIALOG @InitDlgHandle
        FROM SERVICE [@InitiatorService] --Here
        TO SERVICE @TargetService
        ON CONTRACT [@BasicContract] --Here
        WITH
            ENCRYPTION = OFF;

SET @Message = (SELECT
                ProcedureName    = @ProcedureName,
                SubProcedureName = @SubProcedureName,
                ProcedureType    = @ProcedureType,
                Inserted         = @Inserted,
                Deleted          = @Deleted
            FOR XML PATH('Request'));

SEND ON CONVERSATION @InitDlgHandle
        MESSAGE TYPE [@RequestMessageType] --Here
        (@Message);

SELECT @Message AS SentRequestMsg;

COMMIT TRANSACTION;

有没有什么方法可以格式化它,这样我就不会在不使用 Dynamic SQL 的情况下收到错误消息?如果我不能通过抑制错误或使用动态 SQL 来确认它与数据库模式匹配,那么它就没有达到目的。

This 有效

BEGIN DIALOG CONVERSATION @Handle
FROM        SERVICE @SvcName
TO          SERVICE @TargetSvc, 'CURRENT DATABASE'
WITH        ENCRYPTION = OFF

尝试 而不是 引用 @InitiatorService@BasicContract@RequestMessageType -- 这毫无意义。

我最终不得不将创建的 scripts/files 导入到我的 Visual Studio 数据库项目中,以使这一切正常工作而不给我语法错误。

我尝试在未选中“仅包含应用程序范围的对象”并选中“包含扩展属性”的情况下提取 DACPAC,但仍然遇到我以前遇到的相同问题。

我向我的消息、服务和合同项目添加了个别 SQL 项。示例:

服务:

CREATE SERVICE [//Web/Queue/InitiatorService]
    ON QUEUE DataQueue;

合同:

CREATE CONTRACT [//Web/Queue/BasicContract]
(
    [//Web/Queue/RequestMessage] SENT BY INITIATOR,
    [//Web/Queue/ReplyMessage] SENT BY TARGET
)

消息类型:

CREATE MESSAGE TYPE [//Web/Queue/ReplyMessage]
       VALIDATION = WELL_FORMED_XML;
CREATE MESSAGE TYPE [//Web/Queue/RequestMessage]
    VALIDATION = WELL_FORMED_XML;

这让我仍然符合数据库架构并避免将来出现错误。我 did 仍然使用 SQLCMD 变量引用目标服务,因此我可以使用相同的名称,除了开发环境和生产环境中的服务。这是我现在发送消息的存储过程:

CREATE PROCEDURE [dbo].[z_Queue_SendMessage]
    @ProcedureName VARCHAR(1000) = 'Procedure', 
    @SubProcedureName VARCHAR(500) = 'SO', 
    @ProcedureType VARCHAR(200) = 'Delete', 
    @Inserted AS XML = NULL, 
    @Deleted AS XML = NULL
AS


DECLARE @InitDlgHandle UNIQUEIDENTIFIER;

DECLARE @RequestMsg NVARCHAR(MAX);
DECLARE @Message XML;

DECLARE @TargetService NVARCHAR(100) = N'//$(WebDb)/Queue/TargetService'

BEGIN TRANSACTION;

BEGIN DIALOG @InitDlgHandle
        FROM SERVICE [//Web/Queue/InitiatorService]
        TO SERVICE @TargetService
        ON CONTRACT [//Web/Queue/BasicContract]
        WITH
            ENCRYPTION = OFF;

SET @Message = (SELECT
                ProcedureName    = @ProcedureName,
                SubProcedureName = @SubProcedureName,
                ProcedureType    = @ProcedureType,
                Inserted         = @Inserted,
                Deleted          = @Deleted
            FOR XML PATH('Request'));

SEND ON CONVERSATION @InitDlgHandle
        MESSAGE TYPE [//Web/Queue/RequestMessage]
        (@Message);

SELECT @Message AS SentRequestMsg;

COMMIT TRANSACTION;