Create Stored Procedure TSQL Tutorial

Create Procedure Example:

Customers Table

CUSTOMER_IDCUSTOMER_NAMECUSTOMER_TYPE
1CUSTOMER_1CC
2CUSTOMER_2I
3CUSTOMER_3SM
4CUSTOMER_4CC

Contracts Table

CONTRACT_IDCUSTOMER_IDAMOUNT
11400
22500
33700
411000
521200
64900
732000
821500

CREATE PROCEDURE SalesByCustomer
  @CustomerName nvarchar(50)
AS
SELECT c.customer_name, sum(ctr.amount) AS TotalAmount
  FROM customers c, contracts ctr
WHERE c.customer_id = ctr.customer_id
     AND c.customer_name = @CustomerName
GROUP BY c.customer_name
ORDER BY c.customer_name
GO

EXEC SalesByCustomer 'CUSTOMER_1'
GO

Customer_NameTotalAmount
CUSTOMER_11400