在 SQL 中使用 SELECT 设置变量值时出错

问题描述

我有以下存储过程,我发现存储过程的问题是使用 select 设置 @DeliveryAddress 变量的值,因此对于所有引发的 Letter Requests,它只保留 Requests 中的最后一个值。我尝试通过显式设置 @DeliveryAddresss Calculation 中使用的每个值来使用 SET,但没有用,因为我最终收到子查询返回多行的错误

我无法理解我应该在这个 SP 中更改什么,以便为 @Requests 中的每个值设置不同的 @deliveryAddress 值并用于插入 LetterRequest 表中。请帮忙。 注意#WorkFlowAcct 是一个临时表,有大约 10 个唯一的 AccountId,对于每个帐户,我们至少有一个债务人。

ALTER PROCEDURE [dbo].[WorkFlow_Action_RequestLetterPref]  
AS
DECLARE @LetterID INTEGER;
DECLARE @LetterType CHAR(3);
DECLARE @LetterDescription VARCHAR(50);
DECLARE @JobName VARCHAR(256);
DECLARE @DateCreated DATETIME;
DECLARE @DeliveryMethod VARCHAR(7);
DECLARE @DeliveryAddress VARCHAR(1023);

SELECT @LetterID = [LetterID],@LetterType = CASE
        WHEN [type] IN ('SIF','PIF','PPS','PDC','ATT','CUS') THEN [type]
        ELSE 'DUN'
    END,@LetterDescription = ISNULL([Description],''),FROM [dbo].[letter]
WHERE [code] = @LetterCode;


DECLARE @Requests TABLE (
    [AccountID] INTEGER NOT NULL,[DebtorID] INTEGER NOT NULL,[Seq] INTEGER NOT NULL,[ErrorMessage] VARCHAR(500) NULL
);


IF @PrimaryDebtor = 1 OR @LetterType IN ('CUS','ATT') BEGIN
    INSERT INTO @Requests ([AccountID],[DebtorID],[Seq])
    SELECT disTINCT [master].[number] AS [AccountID],[Debtors].[DebtorID] AS [DebtorID],[Debtors].[Seq] AS [Seq],FROM #WorkFlowAcct AS [WorkFlowAcct]
    INNER JOIN [dbo].[master] WITH (NOLOCK)
    ON [WorkFlowAcct].[AccountID] = [master].[number]
    INNER JOIN [dbo].[customer] WITH (NOLOCK)
    ON [master].[customer] = [customer].[customer]
    INNER JOIN [dbo].[Debtors] WITH (NOLOCK)
    ON [master].[number] = [Debtors].[number]
    AND [Debtors].[Seq] = [master].[PSeq]
    LEFT OUTER JOIN [dbo].[DebtorAttorneys]
    ON [Debtors].[DebtorID] = [DebtorAttorneys].[DebtorID];
END;


DECLARE @Street1 VARCHAR(512);
DECLARE @Street2 VARCHAR(512);
DECLARE @City VARCHAR(512);
DECLARE @Country VARCHAR(512);
DECLARE @Zip VARCHAR(512);


IF @Pref = 'Letter'
BEGIN
    SET @DeliveryMethod = 'Letter';
    SELECT @DeliveryAddress = [Street1] + ' ' + [Street2] + ' ' + [City] + ' ' + [Zipcode] + ' ' + [Country],@Street1 = [Street1],@Street2 = [Street2],@City = [City],@Country = [Country],@Zip = [ZipCode] FROM [Debtors] inner join @Requests AS Requests on [Debtors].[DebtorID] = Requests.[DebtorID] AND [Street1] IS NOT NULL;
    IF @Street2 IS NULL
    BEGIN
        SET @DeliveryAddress = @Street1 + ' ' + @City + ' ' + @Zip + ' ' + @Country;
    END
END

SET @DateCreated = GETDATE();
SET @JobName = 'WorkFlow_' + CAST(NEWID() AS CHAR(36)) + CAST(NEWID() AS CHAR(36)) + CAST(NEWID() AS CHAR(36)) + CONVERT(VARCHAR(50),GETDATE(),126);

BEGIN TRANSACTION;

--Updated params for current Letter Request Table
INSERT INTO [dbo].[LetterRequest] ([AccountID],[LetterID],[LetterCode],[DeliveryMethod],[DeliveryAddress])
SELECT [Requests].[AccountID],[Requests].[CustomerCode],@LetterID AS [LetterID],@LetterCode AS [LetterCode],@DeliveryMethod AS [DeliveryMethod],@DeliveryAddress AS [DeliveryAddress]
FROM @Requests AS [Requests]
INNER JOIN @AllowedCustomers AS [AllowedCustomers]
ON [Requests].[CustomerCode] = [AllowedCustomers].[CustomerCode]
WHERE [Requests].[ErrorMessage] IS NULL;

COMMIT TRANSACTION;

RETURN 0;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)