Stored Procedure in Fabric Warehouse – Input & Output Parameters SQL Demo
In this Microsoft Fabric tutorial, you'll learn how to create and execute a stored procedure with input and output parameters. This is especially useful when you want to encapsulate query logic and reuse it across reports, data pipelines, and dashboards.
๐ง What is a Stored Procedure?
A stored procedure is a reusable set of SQL statements saved in the database. It can take input parameters, return output values, and perform operations like reading or modifying data.
✅ Benefits of Stored Procedures:
- Encapsulates business logic
- Promotes reuse and consistency
- Supports input and output parameters
- Can include conditional and complex logic
๐️ Step 1: Create a Sample Table
CREATE TABLE dbo.Orders (
OrderID INT,
CustomerName VARCHAR(100),
OrderAmount DECIMAL(10,2)
);
๐พ Step 2: Insert Sample Data
INSERT INTO dbo.Orders VALUES
(1, 'Aamir', 500.00),
(2, 'Sara', 750.00),
(3, 'John', 1200.00);
⚙️ Step 3: Create the Stored Procedure
This procedure accepts a customer name as input and returns the total order amount as output:
ALTER PROCEDURE dbo.GetCustomerTotalOrders
@CustomerName VARCHAR(100),
@TotalAmount DECIMAL(10,2) OUTPUT
AS
BEGIN
SELECT @TotalAmount = SUM(OrderAmount)
FROM dbo.Orders
WHERE CustomerName = @CustomerName;
END;
๐ Step 4: Execute the Stored Procedure
-- Declare output variable
DECLARE @Total DECIMAL(10,2);
-- Execute with input and capture output
EXEC dbo.GetCustomerTotalOrders
@CustomerName = 'Aamir',
@TotalAmount = @Total OUTPUT;
-- Display the result
SELECT 'Total Order Amount for Aamir' AS Description, @Total AS TotalAmount;
๐งน Optional Cleanup
-- DROP PROCEDURE dbo.GetCustomerTotalOrders;
-- DROP TABLE dbo.Orders;
๐ View Final Orders Table
SELECT * FROM dbo.Orders;