Instead of a temp table, you can use a table variable.
declare @Temp TABLE (ID int identity, ProductId int)
insert into @Temp(ProductId)
select ProductId
from OrderProduct
where OrderId = @OrderId
But you can rewrite your function without a loop.
Something like this should do what you want.
create function IF_GetOrderProducts
(
@OrderId int
)
returns varchar(500)
as
begin
return
(
select Name+'<br/>'
from Product as P
inner join OrderProduct as OP
on P.ProductId = OP.ProductId
where OP.OrderId = @OrderId
for xml path(''), type
).value('.', 'varchar(500)')
end
source
share