Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to: ✅ SQL analytics endpoint and Warehouse in Microsoft Fabric
An inline table-valued function (TVF) is a named T-SQL query that accepts input parameters and
returns a table. Use it as a query source in Fabric Warehouse or the
SQL analytics endpoint.
The returned rows and columns come from a single SELECT statement, not a stored copy of data.
You can create, alter, and drop inline TVFs in both Warehouse and the SQL analytics endpoint. Creating a function in the SQL analytics endpoint doesn't change the underlying Delta tables or make their data writable.
Why use inline table-valued functions?
Use an inline TVF when you want to pass inputs to a query and use its rows in another query:
- Parameterize a query. Run joins, filters, and aggregations for different dates or customers.
- Build on the result. Join the function's results to other sources, or pass values from
each source row through
APPLY. - Define security predicates. Use an inline TVF as a filter predicate in a row-level security policy.
Unlike a view, an inline TVF accepts parameters. Unlike a stored procedure, its result can be a source in the calling query. Use a scalar function when you need one value in an expression.
Supported syntax
Use the following syntax to define and query inline TVFs, and to manage their permissions.
Create or alter a function
For T-SQL syntax, see CREATE FUNCTION (Transact-SQL) and ALTER FUNCTION (Transact-SQL).
CREATE [ OR ALTER ] FUNCTION [ schema_name. ] function_name
(
[ @parameter_name [ AS ] parameter_data_type [ = default ] [ ,...n ] ]
)
RETURNS TABLE
[ WITH SCHEMABINDING ]
[ AS ]
RETURN [ ( ] <select_statement> [ ) ]
[;]
ALTER FUNCTION [ schema_name. ] function_name
(
[ @parameter_name [ AS ] parameter_data_type [ = default ] [ ,...n ] ]
)
RETURNS TABLE
[ WITH SCHEMABINDING ]
[ AS ]
RETURN [ ( ] <select_statement> [ ) ]
[;]
Square brackets indicate optional syntax, braces indicate alternatives, and n indicates
repetition. Use CREATE OR ALTER to create a function or update its definition.
ALTER requires an existing function. Include parameter-list parentheses even if you don't
define parameters. You can omit parentheses around the returned query, but if you include
them, use them as a pair.
ENCRYPTION, EXECUTE AS, and NATIVE_COMPILATION aren't supported. INLINE = AUTO,
RETURNS NULL ON NULL INPUT, and CALLED ON NULL INPUT are scalar-function options, not
inline TVF options.
Drop a function
For T-SQL syntax, see DROP FUNCTION (Transact-SQL).
DROP FUNCTION [ IF EXISTS ] [ schema_name. ] function_name
[;]
Query a function
Use an inline TVF as a source in FROM, not as a scalar expression or with EXECUTE.
This abbreviated form shows a direct call with literals or variables. For full query syntax, see
SELECT (Transact-SQL)
and FROM (Transact-SQL).
SELECT <select_list>
FROM schema_name.function_name
( [ { literal | @variable | DEFAULT } [ ,...n ] ] ) [ AS table_alias ]
[ WHERE <search_condition> ]
[ ORDER BY <order_by_expression> [ ASC | DESC ] [ ,...n ] ]
[;]
Grant permissions on a function
Use these forms to grant permissions for querying and managing an inline TVF. For T-SQL syntax, see GRANT object permissions (Transact-SQL).
GRANT <function_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] function_name
TO principal [ ,...n ]
[ WITH GRANT OPTION ]
[;]
<function_permission> ::=
ALTER | CONTROL | REFERENCES | SELECT | TAKE OWNERSHIP | VIEW DEFINITION
WITH GRANT OPTION lets the recipient grant the permission to others. Querying an inline TVF
directly requires SELECT, not the EXECUTE permission used for scalar functions.
Deny permissions on a function
Use the same function_permission list as for GRANT. For T-SQL syntax, see
DENY (Transact-SQL).
DENY <function_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] function_name
TO principal [ ,...n ]
[ CASCADE ]
[;]
CASCADE also denies the permission to principals who received it from the target principal.
It's required when the target holds the permission with GRANT OPTION.
Revoke permissions on a function
Use the same function_permission list as for GRANT. For T-SQL syntax, see
REVOKE (Transact-SQL).
REVOKE <function_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] function_name
FROM principal [ ,...n ]
[ CASCADE ]
[;]
CASCADE also revokes permissions that the recipient delegated to others.
Create, alter, and drop a function
Use schema-qualified names. When you alter a function, include the complete parameter list,
options, and query. ALTER and CREATE OR ALTER preserve object-level permissions.
Changing parameters or returned columns can break callers. If you change the returned columns,
review their column-level grants.
Keep each function definition in its own batch. GO separates batches in SQL tools.
If you're using an application driver, submit each batch separately without sending GO.
For example, create or update a function that returns integers from 1 through @Stop:
CREATE OR ALTER FUNCTION dbo.fn_GetRows (@Stop int)
RETURNS TABLE
AS
RETURN
(
SELECT s.value AS Result
FROM GENERATE_SERIES(1, COALESCE(@Stop, 0), 1) AS s
);
GO
GENERATE_SERIES (Transact-SQL)
provides the rows. By specifying a positive step (1), you get no rows when the stop value
is below 1, rather than a descending series.
Use DROP FUNCTION IF EXISTS to remove a function without an error if it doesn't exist.
Dropping the function removes its permissions and can break callers. Schema-bound objects that
depend on the function can prevent you from dropping it. Alter or drop those objects first.
If you recreate the function, reapply its grants.
DROP FUNCTION IF EXISTS dbo.fn_ObsoleteTableFunction;
ALTER FUNCTION can't change an inline TVF into a scalar function or a multi-statement TVF.
Function definition
The query after RETURN defines the table's columns and rows. Within the supported
T-SQL surface area, you can:
- Read tables, views, and other inline TVFs.
- Join sources, filter rows, calculate columns, and aggregate data.
- Use nonrecursive CTEs and set operators such as
UNIONandUNION ALL.
List columns explicitly and give each calculated expression a name. Each result column needs a unique name. The query determines the data types, so you don't declare a separate return table.
An inline TVF doesn't have a BEGIN...END body, local variable declarations, loops, or multiple
procedural statements. SET NOCOUNT ON, dynamic SQL, stored-procedure calls, and data changes
aren't supported in its definition. Use a scalar function for procedural calculations that return one value, or a stored procedure for multi-step operations.
Fabric optimizes the function's query along with the calling statement. A function doesn't precompute results or automatically improve performance. Follow the performance guidelines.
Schema binding
WITH SCHEMABINDING prevents SQL changes to referenced objects that would invalidate the
function. Use two-part names and keep references in the same database. Any UDFs you reference
must also be schema-bound. To create the function, you need REFERENCES on referenced objects.
For example, if dbo.Sales already has SaleId, CustomerId, and Amount columns:
CREATE OR ALTER FUNCTION dbo.fn_SalesByCustomer (@CustomerId int)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT SaleId, Amount
FROM dbo.Sales
WHERE CustomerId = @CustomerId
);
GO
To keep schema binding when you update the function, include SCHEMABINDING in the new
definition. Before you make an incompatible schema change, alter or drop the dependent
schema-bound function.
For lakehouse schema changes, also account for SQL analytics endpoint metadata sync.
Nested calls and limitations
An inline TVF can query another inline TVF:
CREATE OR ALTER FUNCTION dbo.fn_GetRowsCaller (@Stop int)
RETURNS TABLE
AS
RETURN
(
SELECT Result
FROM dbo.fn_GetRows(@Stop)
);
GO
Don't create circular references between functions. Recursive queries aren't supported. You can't create multi-statement TVFs that populate a return table variable or CLR table-valued functions. See the T-SQL surface area and migration limitations.
Don't apply scalar UDF inlining options to inline TVFs. If a TVF calls a scalar UDF, that scalar function still has its own inlining requirements.
Query inline table-valued functions
Query the function with SELECT (Transact-SQL):
SELECT Result
FROM dbo.fn_GetRows(5);
Pass parameter values
Specify input types explicitly, including string length or decimal precision and scale. For supported types, see Data types. Pass arguments in parameter order. Parameters represent values, not table or column names.
You can pass literals or variables in a direct call. Use APPLY if you need to pass column
values from another query source. For example, pass a variable to the function defined earlier:
DECLARE @Input int = 5;
SELECT Result
FROM dbo.fn_GetRows(@Input);
Use default parameter values
A parameter default is a constant or NULL. Pass DEFAULT to use it; unlike a stored
procedure, you can't omit the argument. Passing NULL supplies a null value, not a request
for the default.
CREATE OR ALTER FUNCTION dbo.fn_GetRowsOrDefault (@Stop int = 10)
RETURNS TABLE
AS
RETURN
(
SELECT s.value AS Result
FROM GENERATE_SERIES(1, COALESCE(@Stop, 0), 1) AS s
);
GO
SELECT Result FROM dbo.fn_GetRowsOrDefault(DEFAULT); -- Returns the numbers 1 through 10.
SELECT Result FROM dbo.fn_GetRowsOrDefault(NULL); -- Returns no rows.
Handle null inputs and empty results
A null argument doesn't automatically produce an empty table. The function's query determines
the result. In dbo.fn_GetRows, COALESCE maps NULL to 0, so the positive-step range
from 1 to 0 contains no rows:
SELECT Result FROM dbo.fn_GetRows(NULL); -- Returns no rows.
SELECT Result FROM dbo.fn_GetRows(0); -- Returns no rows.
Use IS NULL, IS NOT NULL, or COALESCE in the query when you need other null handling.
An empty result still has the function's defined columns. It isn't a scalar NULL result.
Combine results with other query sources
Use a TVF in a join or apply filters and sorting to its results in the calling query.
CROSS APPLY passes values from each left-side row to the function and returns matching rows.
For the function defined earlier:
SELECT s.value AS InputValue, f.Result
FROM GENERATE_SERIES(-1, 3, 1) AS s
CROSS APPLY dbo.fn_GetRows(s.value) AS f;
The function returns one, two, and three rows for inputs 1, 2, and 3, respectively.
CROSS APPLY omits inputs -1 and 0. Replace it with OUTER APPLY to keep those inputs,
with NULL in f.Result when the function returns no rows.
For details, see
FROM (Transact-SQL).
Put ORDER BY in your outer query when order matters. In Warehouse, you can also use a
function's results as a source for supported data-changing statements, but not as a writable
target. The SQL analytics endpoint remains read-only for persisted table data.
Security and permissions
To connect to the warehouse or SQL analytics endpoint, you need at least Read permission on the item, either directly or through a workspace role. See Fabric security and SQL granular permissions.
If you're a workspace Admin, Member, or Contributor, you can create functions.
| Operation | Required SQL permission |
|---|---|
| Create a function | CREATE FUNCTION in the database and ALTER on the target schema. |
| Alter a function | ALTER on the function. |
| Drop a function | CONTROL on the function or ALTER on its schema. |
| Query a function directly | SELECT on the function. Access to referenced objects also depends on ownership chaining. |
| Bind a function to referenced objects | REFERENCES on those objects, in addition to the creation or alteration permissions. |
| Read a function definition | VIEW DEFINITION on the function. |
| Deny or revoke a permission | CONTROL on the function. |
You can also meet these requirements through higher-level permissions. To grant a permission,
you need GRANT OPTION or a higher permission that lets you grant it.
For example, grant query and definition access separately. Replace TableFunctionReaders
with the name of your Microsoft Entra user or group.
GRANT SELECT ON OBJECT::dbo.fn_GetRows TO [TableFunctionReaders];
GRANT VIEW DEFINITION ON OBJECT::dbo.fn_GetRows TO [TableFunctionReaders];
You can't run CREATE USER. Fabric creates the database user automatically when you grant
or deny permissions.
Use DENY to block a permission, including grants inherited through roles or groups.
It doesn't restrict the function's owner. For example, block direct queries:
DENY SELECT ON OBJECT::dbo.fn_GetRows TO [TableFunctionReaders];
Use REVOKE to remove an explicit GRANT or DENY without blocking other grants.
For example, remove the preceding denial:
REVOKE SELECT ON OBJECT::dbo.fn_GetRows FROM [TableFunctionReaders];
Removing the denial doesn't restore the earlier explicit GRANT. Other grants can still
allow access.
A column-level GRANT can take precedence over an object-level DENY; see
GRANT (Transact-SQL).
Ownership chaining can allow access to referenced tables when the objects have the same owner. If the ownership chain is broken, you need additional permissions. A filtered function doesn't remove table access you already have.
To enforce row-level security on a table, use an enabled security policy with an inline TVF predicate; creating or querying a filtering function alone doesn't enforce a policy. For an example, see Implement row-level security.
SQL permissions don't secure other access paths to the same lakehouse data, such as Spark. See SQL analytics endpoint security.
Metadata and function definitions
Query these catalog views and functions in the database that contains the function:
| Catalog view or function | Information |
|---|---|
| sys.objects (Transact-SQL) | Inline TVFs have type = 'IF' and type_desc = 'SQL_INLINE_TABLE_VALUED_FUNCTION'. Includes object IDs, names, schema IDs, and creation and modification dates. |
| sys.parameters (Transact-SQL) | Input parameter names, order, types, lengths, precision, and scale. The scalar return-value row with parameter_id = 0 doesn't describe a TVF's returned columns. |
| sys.columns (Transact-SQL) | Returned column names, order, type IDs, lengths, precision, scale, and nullability. Filter by the function's object_id. |
| sys.sql_modules (Transact-SQL) | Function source text in definition and schema-binding status in is_schema_bound. |
| OBJECT_ID (Transact-SQL) | Returns a function's object ID from its schema-qualified name. Specify 'IF' for an inline TVF. |
| OBJECT_NAME (Transact-SQL) | Returns a function's name, without its schema, from its object ID. |
| OBJECT_SCHEMA_NAME (Transact-SQL) | Returns a function's schema name from its object ID. |
| SCHEMA_NAME (Transact-SQL) | Returns a schema name from a schema ID, such as sys.objects.schema_id. |
| OBJECT_DEFINITION (Transact-SQL) | Returns a function's T-SQL definition from its object ID, not its name. |
| sys.sql_expression_dependencies (Transact-SQL) | Recorded references to tables, views, and other functions. |
For example, resolve the function's ID, then retrieve its schema, name, and definition:
DECLARE @FunctionId int = OBJECT_ID('dbo.fn_GetRows', 'IF');
SELECT @FunctionId AS FunctionId,
OBJECT_SCHEMA_NAME(@FunctionId) AS SchemaName,
OBJECT_NAME(@FunctionId) AS FunctionName,
OBJECT_DEFINITION(@FunctionId) AS FunctionDefinition;
The metadata you can see depends on the objects you own and the permissions you have.
Name and definition lookups can return NULL if the function doesn't exist or you don't
have metadata permissions.
SELECT doesn't let you read the definition. These lookups use the current database.
Object IDs aren't globally unique.
To find a T-SQL function's parameter defaults, read its definition. sys.parameters doesn't
store them in default_value.
In sys.sql_expression_dependencies, filter referencing_id by the function's object ID to
find what it uses, or referenced_id to find resolved dependents in the current database.
Cross-database or unresolved references can have a NULL referenced_id; inspect
referenced_database_name, referenced_schema_name, and referenced_entity_name as well.
To read this catalog view, you need database-level VIEW DEFINITION and SELECT on
sys.sql_expression_dependencies.
Related content
- T-SQL surface area in Fabric Data Warehouse
- SQL granular permissions in Microsoft Fabric
- Stored procedures
- Views
- Scalar functions
- External functions
- Develop warehouse projects in Visual Studio Code
- Row-level security
- Implement row-level security
- CREATE FUNCTION (Transact-SQL)
- ALTER FUNCTION (Transact-SQL)
- DROP FUNCTION (Transact-SQL)
- SELECT (Transact-SQL)
- FROM (Transact-SQL)
- GENERATE_SERIES (Transact-SQL)
- GRANT object permissions (Transact-SQL)
- DENY (Transact-SQL)
- REVOKE (Transact-SQL)