我有以下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”获取数据。它存储在一个varibale“@ Id1”中,这里变量@ id1将返回多个值。此代码无法读取多个值,如何通过循环等逐个读取此值。我想将此值放入打击查询中以获取数据。 谢谢
答案 0 :(得分: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