Create stored procedures in T-SQL
In this lab, you’ll use T-SQL statements to create and understand stored procedure techniques in the adventureworks database. For your reference, the following diagram shows the tables in the database (you may need to resize the pane to see them clearly).
Note: If you’re familiar with the standard AdventureWorks sample database, you may notice that in this lab we are using a simplified version that makes it easier to focus on learning Transact-SQL syntax.
Create and execute stored procedures
- Start Azure Data Studio.
- From the Servers pane, double-click the AdventureWorks connection. A green dot will appear when the connection is successful.
- Right click on the AdventureWorks connection and select New Query. A new query window is displayed with a connection to the AdventureWorks database.
-
Type the following T-SQL code:
CREATE PROCEDURE SalesLT.TopProducts AS SELECT TOP(10) name, listprice FROM SalesLT.Product GROUP BY name, listprice ORDER BY listprice DESC;
- Select ⏵Run. You’ve created a stored procedure named SalesLT.TopProducts.
-
In the query pane, type the following T-SQL code after the previous code:
EXECUTE SalesLT.TopProducts;
- Highlight the written T-SQL code and click ⏵Run. You’ve now executed the stored procedure.
-
Now modify the stored procedure so that it returns only products from a specific product category by adding an input parameter. In the query pane, type the following T-SQL code:
ALTER PROCEDURE SalesLT.TopProducts @ProductCategoryID int AS SELECT TOP(10) name, listprice FROM SalesLT.Product WHERE ProductCategoryID = @ProductCategoryID GROUP BY name, listprice ORDER BY listprice DESC;
-
In the query pane, type the following T-SQL code:
EXECUTE SalesLT.TopProducts @ProductCategoryID = 18;
- Highlight the written T-SQL code and click ⏵Run to execute the stored procedure, passing the parameter value by name.
Challenge
- Pass a value to the stored procedure by position instead of by name. Try Product Category 41.
Challenge answer
EXECUTE SalesLT.TopProducts 41;
Create an inline table valued function
-
In the query pane, type the following T-SQL code:
CREATE FUNCTION SalesLT.GetFreightbyCustomer (@orderyear AS INT) RETURNS TABLE AS RETURN SELECT customerid, SUM(freight) AS totalfreight FROM SalesLT.SalesOrderHeader WHERE YEAR(orderdate) = @orderyear GROUP BY customerid;
-
Highlight the written T-SQL code and click ⏵Run to create the table-valued function.
Challenge
- Run the table-valued function to return data for the year 2008.
Challenge answer
SELECT * FROM SalesLT.GetFreightbyCustomer(2008)