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
A stored procedure is a named group of T-SQL statements that you save in Fabric Warehouse or
the SQL analytics endpoint.
Run it with EXECUTE or EXEC. Use procedures to encapsulate business logic and share queries
and transformations across applications and pipelines.
The SQL analytics endpoint supports read-only access to table data; a procedure can't create or modify persisted tables or their data.
Why use stored procedures?
Use stored procedures when your applications or pipelines need to run a business operation:
- Run a workflow in one call. Combine validation, branching, queries, and transformations.
- Reuse a workflow with different inputs. Run it for different dates, customers, or batches.
- Expose approved actions. Grant access to specific operations through
EXECUTE. - Update business rules. Change validation or transformation logic without changing the calling interface.
Procedures don't automatically improve query performance. Follow the performance guidelines.
Supported syntax
Use the following syntax to define and run procedures, and to manage their permissions.
Create or alter a procedure
For T-SQL syntax, see CREATE PROCEDURE (Transact-SQL) and ALTER PROCEDURE (Transact-SQL).
CREATE [ OR ALTER ] { PROC | PROCEDURE }
[ schema_name. ] procedure_name
[ @parameter_name data_type [ OUT | OUTPUT ] [ = default ] [ ,...n ] ]
[ WITH { ENCRYPTION | RECOMPILE } [ ,...n ] ]
AS
[ BEGIN ]
sql_statement [;] [ ...n ]
[ END ]
[;]
ALTER { PROC | PROCEDURE }
[ schema_name. ] procedure_name
[ @parameter_name data_type [ OUT | OUTPUT ] [ = default ] [ ,...n ] ]
[ WITH { ENCRYPTION | RECOMPILE } [ ,...n ] ]
AS
[ BEGIN ]
sql_statement [;] [ ...n ]
[ END ]
[;]
Square brackets indicate optional syntax, braces indicate alternatives, and n indicates
repetition. Use CREATE OR ALTER to create a procedure or update its definition.
ALTER requires an existing procedure. You can omit BEGIN and END, and use either
PROC or PROCEDURE.
Note
CLR procedures (AS EXTERNAL NAME) aren't supported. For custom Python logic, use
Fabric user data functions (preview).
Place procedure options in WITH before AS. EXECUTE AS, FOR REPLICATION,
NATIVE_COMPILATION, and SCHEMABINDING aren't supported.
If you alter a procedure that uses ENCRYPTION or RECOMPILE, include the option again to
retain it.
Drop a procedure
For T-SQL syntax, see DROP PROCEDURE (Transact-SQL).
DROP { PROC | PROCEDURE } [ IF EXISTS ]
[ schema_name. ] procedure_name
[;]
Execute a procedure
For T-SQL syntax, see EXECUTE (Transact-SQL).
{ EXEC | EXECUTE } [ @return_status = ]
[ schema_name. ] procedure_name
[ [ @parameter_name = ] { literal | @variable [ OUT | OUTPUT ] | DEFAULT } [ ,...n ] ]
[ WITH <execute_option> [ ,...n ] ]
[;]
<execute_option> ::=
RECOMPILE
| RESULT SETS UNDEFINED
| RESULT SETS NONE
| RESULT SETS ( <result_set_definition> [ ,...n ] )
<result_set_definition> ::=
( { column_name data_type [ COLLATE collation_name ] [ NULL | NOT NULL ] } [ ,...n ] )
RECOMPILE compiles a fresh plan for the call. RESULT SETS specifies the expected result-set
metadata; UNDEFINED leaves it unchanged, and NONE rejects returned result sets.
Grant permissions on a procedure
For T-SQL syntax, see GRANT object permissions (Transact-SQL).
GRANT <procedure_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] procedure_name
TO principal [ ,...n ]
[ WITH GRANT OPTION ]
[;]
<procedure_permission> ::=
ALTER | CONTROL | EXECUTE | TAKE OWNERSHIP | VIEW DEFINITION
WITH GRANT OPTION lets the recipient grant the permission to others. Table permissions such
as SELECT, INSERT, UPDATE, and DELETE don't apply to procedures.
Deny permissions on a procedure
Use the same procedure_permission list as for GRANT. For T-SQL syntax, see
DENY (Transact-SQL).
DENY <procedure_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] procedure_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 procedure
Use the same procedure_permission list as for GRANT. For T-SQL syntax, see
REVOKE (Transact-SQL).
REVOKE <procedure_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] procedure_name
FROM principal [ ,...n ]
[ CASCADE ]
[;]
CASCADE also revokes permissions delegated by the recipient.
Create, alter, and drop a procedure
Use schema-qualified names. Avoid the sp_ prefix reserved for system procedures.
When you alter a procedure, include the complete parameter list, options, and body.
ALTER and CREATE OR ALTER preserve object-level permissions.
Keep each procedure 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 procedure with one input parameter:
CREATE OR ALTER PROCEDURE dbo.usp_GetValue @Value int
AS
SELECT @Value AS Result;
GO
Use DROP PROCEDURE IF EXISTS to remove a procedure without an error if it doesn't exist.
Dropping the procedure removes its object-level permissions and can break callers that depend
on it. If you recreate the procedure, reapply its grants.
DROP PROCEDURE IF EXISTS dbo.usp_ObsoleteExample;
For an end-to-end example, see Tutorial: Transform data with a stored procedure.
Procedure body
The procedure body starts after AS and contains one or more T-SQL statements. Use BEGIN and END
to group statements. Within the supported T-SQL surface area, you can:
- Query data with
SELECT, joins, aggregations, and nonrecursive CTEs. - Declare and assign local variables, branch with
IF...ELSE, loop withWHILE, and exit withRETURN. - In Warehouse, modify data with
INSERT,UPDATE,DELETE, andMERGE, and create, alter, or drop tables. The SQL analytics endpoint doesn't support these changes to persisted tables.
You can't put CREATE or ALTER statements for procedures, views, or functions directly in
the body. CREATE SCHEMA and USE aren't supported in the body either. Deploy these objects
in separate batches. GO is a client-side batch separator, not a statement in a procedure body.
For details, see
CREATE PROCEDURE (Transact-SQL).
Session settings
Place runtime SET statements in the body and creation-time settings in a preceding batch:
| Setting | Support | Description |
|---|---|---|
| SET NOCOUNT ON (Transact-SQL) | Supported. | Suppresses row-count messages, not SELECT results or updates to @@ROWCOUNT. Put it at the start of the body. |
| SET QUOTED_IDENTIFIER ON (Transact-SQL) | Supported. | Treats double-quoted text as identifiers and single-quoted text as literals. Set it before creating or altering the procedure, not inside its body. Bracketed identifiers aren't affected. |
| SET ANSI_WARNINGS ON (Transact-SQL) | Supported. | Enables ANSI warning and error behavior, including warnings when aggregates eliminate nulls. |
| SET ARITHABORT ON (Transact-SQL) | Supported. | Controls arithmetic overflow and divide-by-zero behavior with ANSI_WARNINGS; it isn't a general transaction rollback setting. |
| SET XACT_ABORT ON (Transact-SQL) | Not supported. | In SQL Server, rolls back the current transaction on runtime errors. |
| SET ROWCOUNT (Transact-SQL) | Not supported. | Use TOP or query predicates to limit rows. |
| SET TRANSACTION ISOLATION LEVEL (Transact-SQL) | Not supported. | Fabric enforces snapshot isolation. |
Fabric saves the ANSI_NULLS and QUOTED_IDENTIFIER settings when you create or alter a
procedure. Use IS NULL and IS NOT NULL for null comparisons, not legacy ANSI_NULLS OFF
behavior. Runtime settings such as NOCOUNT revert when the procedure returns.
For T-SQL syntax, see SET statements (Transact-SQL).
Error handling and transactions
Validate inputs, raise errors with THROW, and handle errors with TRY...CATCH. In Warehouse,
use an explicit transaction for changes that must succeed or fail together; a procedure call
alone isn't atomic. See Transactions.
Nested and recursive calls
Call another procedure with EXECUTE or EXEC inside the caller's body:
CREATE OR ALTER PROCEDURE dbo.usp_GetValueCaller @Value int
AS
EXEC dbo.usp_GetValue @Value = @Value;
GO
You can nest procedure calls up to 32 levels. If you exceed the limit, the entire call chain fails. Direct recursion (a procedure calls itself) and indirect recursion (a call chain returns to an earlier procedure) count toward the same limit. Include a termination condition for recursive calls. See CREATE PROCEDURE (Transact-SQL).
Procedure recursion is separate from recursive CTEs, which aren't supported. See the T-SQL surface area.
Execute stored procedures
Run a procedure with EXECUTE or EXEC. For T-SQL syntax, see
EXECUTE (Transact-SQL).
In this example, dbo.usp_MyProcedure is a placeholder for an existing procedure that doesn't
require arguments:
EXEC dbo.usp_MyProcedure;
Pass parameter values
When you define a parameter, use an @ prefix and specify its data type, including string
length or decimal precision and scale. Match the type to your source data. You can use some
data types for parameters and variables even though they're not supported for table columns.
For details, see Data types.
Pass input values as literals or variables. Prefer named arguments. If you pass arguments by position, follow the parameter declaration order. After the first named argument, you must name all remaining arguments.
Note
Expressions such as 20 + 5 and function calls such as GETDATE() aren't valid arguments
directly in a procedure call. Assign the result to a variable, then pass the variable.
For the dbo.usp_GetValue procedure defined earlier:
EXEC dbo.usp_GetValue @Value = 25; -- Returns 25.
DECLARE @Input int = 20 + 5;
EXEC dbo.usp_GetValue @Value = @Input; -- Returns 25.
For further examples, see Specify parameters (Transact-SQL).
Use default parameter values
A parameter default must be a constant or NULL. Omit an optional parameter or pass DEFAULT
to use its default. Passing NULL supplies a null value, not a request for the default.
CREATE OR ALTER PROCEDURE dbo.usp_GetValueOrDefault @Value int = 10
AS
SELECT @Value AS Result;
GO
EXEC dbo.usp_GetValueOrDefault; -- Returns 10.
EXEC dbo.usp_GetValueOrDefault @Value = DEFAULT; -- Returns 10.
EXEC dbo.usp_GetValueOrDefault @Value = NULL; -- Returns NULL.
Return data
A procedure can return data in three ways:
| Mechanism | Use |
|---|---|
| Result set | Return rows from a SELECT statement to the caller. A procedure can return more than one result set. |
OUTPUT parameter |
Return a scalar value, such as a total or row count, into a caller variable. |
RETURN code |
Return an integer status code. Use result sets or output parameters for business data instead. |
For more information, see Return data from a stored procedure (Transact-SQL).
Use output parameters
Specify OUTPUT in both the definition and the call. Pass a variable to capture the value:
CREATE OR ALTER PROCEDURE dbo.usp_GetOutput @Value int, @Result int OUTPUT
AS
SET @Result = @Value;
GO
DECLARE @OutputValue int;
EXEC dbo.usp_GetOutput @Value = 25, @Result = @OutputValue OUTPUT;
SELECT @OutputValue AS OutputValue; -- Returns 25.
Use return codes
Capture the integer status code in a variable; it isn't a result set:
CREATE OR ALTER PROCEDURE dbo.usp_ReturnStatus
AS
RETURN 0;
GO
DECLARE @Status int;
EXEC @Status = dbo.usp_ReturnStatus;
SELECT @Status AS ReturnCode; -- Returns 0.
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 procedures.
| Operation | Required SQL permission |
|---|---|
| Create a procedure | CREATE PROCEDURE in the database and ALTER on the target schema. |
| Alter a procedure | ALTER on the procedure. |
| Drop a procedure | CONTROL on the procedure or ALTER on its schema. |
| Run a procedure | EXECUTE on the procedure. Permissions on statements and referenced objects also depend on ownership chaining. |
| Read a procedure definition | VIEW DEFINITION on the procedure. |
| Deny or revoke a permission | CONTROL on the procedure. |
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 execution and definition access separately. Replace ProcedureReaders
with the name of your Microsoft Entra user or group.
GRANT EXECUTE ON OBJECT::dbo.usp_GetValue TO [ProcedureReaders];
GRANT VIEW DEFINITION ON OBJECT::dbo.usp_GetValue TO [ProcedureReaders];
You can't run CREATE USER. Fabric creates the database user automatically when you grant
or deny permissions.
Prefer object-level grants. A schema-level EXECUTE grant includes current and future
executable objects in that schema.
Use DENY to block a permission, including grants inherited through roles or groups.
It doesn't restrict the procedure's owner. For example, block execution:
DENY EXECUTE ON OBJECT::dbo.usp_GetValue TO [ProcedureReaders];
Use REVOKE to remove an explicit GRANT or DENY without blocking other grants.
For example, remove the preceding denial:
REVOKE EXECUTE ON OBJECT::dbo.usp_GetValue FROM [ProcedureReaders];
Removing the denial doesn't restore the earlier explicit GRANT. Other grants can still
allow access.
Ownership chaining can allow access to tables through static SQL when the procedure and tables have the same owner. Dynamic SQL and broken ownership chains require separate permission checks. See Permissions (Transact-SQL).
SQL permissions don't secure other access paths to the same lakehouse data, such as Spark. See SQL analytics endpoint security.
Use parameters for values to avoid SQL injection (Transact-SQL). Parameters can't replace table or column names; concatenating untrusted input is still unsafe.
Metadata and procedure definitions
Query these catalog views and functions in the database that contains the procedure:
| Catalog view or function | Information |
|---|---|
| sys.objects (Transact-SQL) | T-SQL stored procedures have type = 'P' and type_desc = 'SQL_STORED_PROCEDURE'. |
| sys.procedures (Transact-SQL) | Procedure object IDs, names, schema IDs, and creation and modification dates. |
| sys.parameters (Transact-SQL) | Parameter order, names, types, lengths, precision, scale, and output direction. |
| sys.sql_modules (Transact-SQL) | Procedure source text in definition, matched to the procedure by object_id. |
| OBJECT_ID (Transact-SQL) | Returns a procedure's object ID from its schema-qualified name. Specify 'P' as the object type. |
| OBJECT_NAME (Transact-SQL) | Returns a procedure's name, without its schema, from its object ID. |
| OBJECT_SCHEMA_NAME (Transact-SQL) | Returns a procedure's schema name from its object ID. |
| SCHEMA_NAME (Transact-SQL) | Returns a schema name from a schema ID, such as sys.procedures.schema_id. |
| OBJECT_DEFINITION (Transact-SQL) | Returns a procedure's T-SQL source text from its object ID, not its name. |
| sys.sql_expression_dependencies (Transact-SQL) | Dependencies recorded from names in procedure definitions, including calls to other procedures. |
For example, resolve a procedure's ID, then retrieve its schema, name, and definition:
DECLARE @ProcedureId int = OBJECT_ID('dbo.usp_GetValue', 'P');
SELECT @ProcedureId AS ProcedureId,
OBJECT_SCHEMA_NAME(@ProcedureId) AS SchemaName,
OBJECT_NAME(@ProcedureId) AS ProcedureName,
OBJECT_DEFINITION(@ProcedureId) AS ProcedureDefinition;
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 procedure doesn't exist or you don't
have metadata permissions.
EXECUTE doesn't let you read the definition. These lookups use the current database.
Object IDs aren't globally unique.
To find a T-SQL procedure's parameter defaults, read its definition. sys.parameters doesn't
store them in default_value.
You can't retrieve the definition if you use WITH ENCRYPTION. Keep your original scripts
in source control.
In sys.sql_expression_dependencies, filter referencing_id by a procedure's object ID to
find its dependencies, or referenced_id to find resolved callers in the current database.
Cross-database, unresolved, or caller-dependent references can have a NULL referenced_id;
also inspect referenced_database_name, referenced_schema_name, referenced_entity_name,
and is_caller_dependent. References constructed in dynamic SQL aren't tracked.
To read this catalog view, you need database-level VIEW DEFINITION and SELECT on
sys.sql_expression_dependencies.
For definition-query examples, see View the definition of a stored procedure (Transact-SQL).
Related content
- Tutorial: Transform data with a stored procedure
- T-SQL surface area in Fabric Data Warehouse
- SQL granular permissions in Microsoft Fabric
- Views
- Scalar functions
- Inline table-valued functions
- External functions
- Develop warehouse projects in Visual Studio Code
- Stored procedures (Transact-SQL)
- CREATE PROCEDURE (Transact-SQL)
- ALTER PROCEDURE (Transact-SQL)
- DROP PROCEDURE (Transact-SQL)
- EXECUTE (Transact-SQL)
- GRANT database permissions (Transact-SQL)
- GRANT schema permissions (Transact-SQL)
- GRANT object permissions (Transact-SQL)
- DENY (Transact-SQL)
- REVOKE (Transact-SQL)
- SET statements (Transact-SQL)