Module - 5 Stored Procedures
Module - 5 Stored Procedures
Procedures
Overview
Creation
Entries into sysobjects
Parsing
and syscomments tables
Execution
(first time or Optimization
recompile)
8082
Connection 1
SELECT *
FROM
dbo.member 24
WHERE Connection 2
member_no = ?
1003
Connection 3
USE Northwind
GO
DROP PROC dbo.OverdueOrders
GO
Stored Procedure Execution
Input Parameters
Methods of Setting Parameter Values
Return Values Using OUTPUT Parameters
Return Values Using the RETURN Statement
Stored Procedure Recompile
Input Parameters
IF @EndDate IS Null
SET @EndDate = GetDate()
Provide
Appropriate IF Datediff(dd,@BeginDate,@EndDate) > 365
BEGIN
Default Values print('The maximum timespan allowed for
this report is one year.')
and Include RETURN
Null Checks END
SELECT O.ShippedDate,O.OrderID,OS.Subtotal,
DATENAME(yy,ShippedDate) AS Year
FROM ORDERS O INNER JOIN [Order Subtotals] OS
ON O.OrderID = OS.OrderID
WHERE O.ShippedDate
BETWEEN @BeginDate AND @EndDate
GO
Methods of Setting Parameter Values
Recompile When
Stored procedure returns widely varying result sets
A new index is added to an underlying table
The parameter value is atypical
Recompile by Using
CREATE PROCEDURE [WITH RECOMPILE]
EXECUTE [WITH RECOMPILE]
sp_recompile
Lesson: Handling Error Messages
Error Messages
Demonstration: Handling Error Messages
Error Messages
The IN Clause
SELECT @SQL =
'SELECT ProductID, ProductName, UnitPrice
FROM Products
WHERE ProductID IN (' + (@ProductIDs) + ')'
Administrative Functions
Lab B: Creating Stored Procedures Using Parameters