0

我想将一个变量初始化到 WHILE 循环中,以便我可以将该变量用于进一步的步骤。
如何为 SELECT 语句初始化 varchar 数组?我这样使用它:

DECLARE @SerialNumber AS VARCHAR

WHILE (
        @SerialNumber = SELECT UnitInContainer.UnitSerialNumber
        FROM UnitInContainer
        WHERE ContainerCode = @Code
        ) IS NOT NULL
    SELECT TOP 1 @CreatedUserName = UnitInStep.CreatedByUserName,
        @CreatedOn = UnitInStep.CreatedOn,
        @DeactivatedOn = UnitInStep.DeactivatedOn,
        @UnitInContainerID = UnitInStep.UnitInContainerID,
        @UnitSerialNumber = UnitInStep.UnitSerialNumber,
        @ContainerCode = UnitInStep.ContainerCode,
        @StepSequence = UnitInStep.StepSequence,
        @RouterItemName = UnitInStep.RouterItemName,
        @RouterRevisionNumber = UnitInStep.RouterRevisionNumber
    FROM UnitInStep
    INNER JOIN RouterStepAttribute
        ON UnitInStep.RouterItemName = RouterStepAttribute.RouterItemName
            AND UnitInStep.RouterRevisionNumber = RouterStepAttribute.RouterRevisionNumber
            AND UnitInStep.StepSequence = RouterStepAttribute.StepSequenc
    WHERE GETDATE() BETWEEN UnitInStep.CreatedOn
            AND UnitInStep.DeactivatedOn RouterStepAttribute.StepTypeStepTypeValue = 'TestChamberAutoResult'
        AND UnitInStep.UnitSerialNumber = @SerialNumber
    END
4

1 回答 1

0

尝试这个:

DECLARE @SerialNumber AS VARCHAR

SELECT @SerialNumber = UnitInContainer.UnitSerialNumber
    FROM UnitInContainer
    WHERE ContainerCode = @Code

WHILE (@SerialNumber is not null)
BEGIN
    -- The longer select using @SerialNumber in the where clause

    SELECT @SerialNumber = UnitInContainer.UnitSerialNumber
        FROM UnitInContainer
        WHERE ContainerCode = @Code
END

它会重复较短的选择,但如果有问题,您可以将其放入函数中。

于 2013-10-11T15:43:23.003 回答