Search notes:

SQL Server: Table valued functions

A table valued function returns a table data type.
The returned values can be used in an select statement as though it was a «real» table.

Two variants

There are two variants of table valued functions:
An ITVF might be more performant than a MSTVF but MSTVFs are more flexible.

Inline table valued functions

An ISTV may only contain exactly one select statement.
create function u.istv (@par int) returns table
as return
  select
    abc,
    def,
    ghi
  from
    t_bla
  where
    id = @par
;

select * from u.istv(42);

Multi-statement table valued functions

create function mstvf(@p integer)
  returns @ret table(col integer)
as
begin
  declare @v integer;

  set @v = 10 * @p; insert into @ret(col) values (@v);
  set @v = 20 * @p; insert into @ret(col) values (@v);

  return;
end;

select * from dbo.mstvf(4);
Combining select and into:
create table src(id integer, val varchar(30));

insert into src values(1, 'one'  );
insert into src values(2, 'two'  );
insert into src values(3, 'three');

go

create function select_into(@p integer)
  returns @ret table(id integer, val varchar(30))
as
begin
  insert into
    @ret
  select *
  from
  src
  where
    id < @p;

  return;
end;
go

select * from dbo.select_into(3);
go

drop function select_into;
go

drop table src;
go
Github repository about-MSSQL, path: /t-sql/user-defined-functions/table-valued/multi-statement/select-into.sql

See also

Returning a «table» in Oracle
The table data type
In order to join with a table valued function that takes a parameter value from another table, the cross apply or outer apply operator is needed.
An example of a very useful table valued function is the built-in function string_split(): it splits a string on a character and returns each part in a single row.
Trying to return a user defined type (UDT)

Index