How call Table Values function from stored procedure

XamDev picture XamDev · Sep 5, 2013 · Viewed 9k times · Source

How to call function which returns table in stored procedure.

I want to do use that returned table from function in stored procedure.

How it is done ?

Answer

i-one picture i-one · Sep 5, 2013

You are not specific in your question, there are different ways a table valued function can be called (depending on need):

create procedure Test
(
    @someParam varchar(50),
    @someParam2 varchar(50)
)
as
begin
    set nocount on

    ...

    select tf.Col1, tf.Col2, tf.Col3, tf.Col4, tf.Col5
    from tableValuedFunction0() tf

    select tf.Col1, tf.Col2
    from tableValuedFunction1(@someParam) tf

    create table #temp (...)

    insert into #temp (...)
    select tf.Col1, tf.Col2, tf.Col3
    from tableValuedFunction2(@someParam, @someParam2) tf

    create table #temp2 (...)

    insert into #temp2 (...)
    select t.SomeCol, tf.Col1, tf.Col2
    from someTable t
        cross apply tableValuedFunction3(@someParam2, t.SomeOtherCol) tf

    update t
    set t.SomeCol = tf.Value
    from someOtherTable t
        join tableValuedFunction4(@someParam2) tf on tf.SomeOtherCol = t.SomeOtherCol

end