Returning a Table from a Function in SQL Server
In SQL Server, functions can directly return a table.
This is simpler than in Oracle because SQL Server has native table return types.
There are two types of table-returning functions:
1. Inline Table-Valued Function (iTVF)
This behaves like a parameterized view.
Concept:
Usage Case:
When you only need to return a filtered or calculated dataset without procedural logic.
2. Multi-Statement Table-Valued Function (mTVF)
This one allows procedural logic.
Concept:
Usage Case:
When you need:
Key Differences vs Oracle
|
Feature |
SQL Server |
Oracle |
|
Return a table directly |
✅ Yes |
❌ No (must use collection types) |
|
Uses explicit row & table types |
❌ Only if needed |
✅ Required |
|
Pipelining (streaming) |
❌ Not used |
✅ Pipelined functions available |
|
Simplicity |
Very simple |
More formal structure, more steps |
|
Performance tuning |
Based on query optimizer |
Depends on normal vs pipelined implementation |
Practical Understanding
|
Function Type |
Best For |
Notes |
|
iTVF (inline) |
High-performance, simple filtering/joins |
Fastest, cleanest |
|
mTVF (multi-statement) |
Complex logic, multiple steps |
More flexible, but slower |
Summary