I have a table IDTable
with one column ID
and also I have one table valued function MyProcedure
that takes a parameter. This parameter is my column ID
. This function returns a table. I need to call this function for every value in column ID
. I have such code:
Declare @ResultTable table(
ClientID nvarchar(50) null,
[Date] datetime null,
Item varchar(100) null
)
Declare @IDTable table(
id int
)
declare @counter as int
set @counter = isnull( (select max(id) from @IDTable),0)
while @counter<>0
begin
set @ID = (select id from @IDTable where id=@counter)
insert into @ResultTable
select ClientID
[Date],
Item
from MyProcedure (@ID)
delete from @IBTable where id=@counter
set @counter=isnull((select max(id) from @IBTable),0)
end
I need to remove while cycle and make set-based alternative. How can I do it?
You can use CROSS APPLY
:
insert into @ResultTable
select b.ClientID,
b.[Date],
b.Item
from @IDTable a
CROSS APPLY MyProcedure(a.id) b