ITE 370: SQL Stored Procedures: Jeffrey P. Landry University of South Alabama
ITE 370: SQL Stored Procedures: Jeffrey P. Landry University of South Alabama
Stored Procedures
A stored procedure is a collection of SQL statements saved under a name and processed by the database server as a unit Stored procedures are precompiled and ready for later use can take and return user-supplied parameters may contain control-of-flow statements (conditionals, loops) are created, modified, called, tested using SQLServer
University of South Alabama School of CIS Last Modified: 4/13/2012
AS
BEGIN -- sproc
-- retrieve information for all Stops on the given bus route, listed -- in chronological order SELECT RouteId, StopOnRouteId, Stop.StopId, Location, ElapsedTime, Outbound FROM StopOnRoute INNER JOIN Stop ON StopOnRoute.StopId=Stop.StopId WHERE RouteId=@RouteId ORDER BY ElapsedTime END -- sproc -- example call for RouteId=2 -- EXEC dbo.GetStopsOnRoute 2 -- execute script GO
-- example call to retrieve number of stops on RouteId=1 /* DECLARE @NumOfStops int SET @NumOfStops = 0 EXEC dbo.GetNumOfStopsOnRoute 1, @StopCount = @NumOfStops OUTPUT
SELECT @NumOfStops AS TotalStops */ GO
10