0

我有以下 TSQL 语句:

declare @id1 int
set  @id1 = (select o.OrderId from [Order] o where o.M_Order_Id=@id)      

set @Message =  select om.Modifier_Id 
                from [Order] o , Order_Modifiers om
                where om.Order_Id=o.OrderId and om.Order_Id = @id1 

我必须根据“OrderId”获取数据。它存储在变量“@Id1”中,这里变量@id1 将返回超过 1 个值。此代码无法读取多个值,如何通过循环等逐一读取此值。我想将此值放入打击查询中以获取数据。谢谢

4

1 回答 1

0

您需要使用游标来实现这一点。

    create procedure test(@id int)
    declare @message_id     nvarchar(4000),
            @id             int,
            @id1            int


        DECLARE @TEMP_table TABLE
        (   id1 int ) 
        INSERT INTO @TEMP_table select o.OrderId from dbo.[Order] o where o.M_Order_Id=@id

        declare @temp_table2 table
        ( id int,message_id int)

        declare c1 cursor for 

                select distinct id1 from @temp_table

        open c1

        fetch next from c1 into @id1

        while @@fetch_status=0
        begin
            insert into @temp_table2    
                select o.order_id,om.Modifier_Id 
                    from [Order] o , Order_Modifiers om
                    where om.Order_Id=o.OrderId and om.Order_Id = @id1
        end

        close c1
        deallocate c1

    select * from @temp_table2
于 2013-06-12T08:37:29.933 回答