Professional Documents
Culture Documents
Create Stored Procedures in T-SQL
Create Stored Procedures in T-SQL
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).
CodeCopy
CREATE PROCEDURE SalesLT.TopProducts AS
SELECT TOP(10) name, listprice
FROM SalesLT.Product
GROUP BY name, listprice
ORDER BY listprice DESC;
CodeCopy
EXECUTE SalesLT.TopProducts;
7. Highlight the written T-SQL code and click ⏵Run. You’ve now executed the
stored procedure.
8. 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:
CodeCopy
CodeCopy
10. Highlight the written T-SQL code and click ⏵Run to execute the stored
procedure, passing the parameter value by name.
Challenge
1. Pass a value to the stored procedure by position instead of by name. Try
Product Category 41.
Challenge answer
CodeCopy
CodeCopy
2. Highlight the written T-SQL code and click ⏵Run to create the table-valued
function.
Challenge
1. Run the table-valued function to return data for the year 2008.
Challenge answer
CodeCopy