0% found this document useful (0 votes)
25 views

ITE 370: SQL Stored Procedures: Jeffrey P. Landry University of South Alabama

This document provides an overview of SQL stored procedures. It defines a stored procedure as a collection of SQL statements saved under a name and processed by the database server as a unit. The document shows the syntax for creating a stored procedure in SQL Server and provides an example of creating a simple stored procedure to retrieve data from a table. It also demonstrates how to call a stored procedure and modify an existing stored procedure. The document describes how to declare and assign values to local variables within a stored procedure.

Uploaded by

AR BI AR
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

ITE 370: SQL Stored Procedures: Jeffrey P. Landry University of South Alabama

This document provides an overview of SQL stored procedures. It defines a stored procedure as a collection of SQL statements saved under a name and processed by the database server as a unit. The document shows the syntax for creating a stored procedure in SQL Server and provides an example of creating a simple stored procedure to retrieve data from a table. It also demonstrates how to call a stored procedure and modify an existing stored procedure. The document describes how to declare and assign values to local variables within a stored procedure.

Uploaded by

AR BI AR
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

ITE 370: SQL Stored Procedures

ITE 370: SQL Stored Procedures


Jeffrey P. Landry University of South Alabama

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

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

ITE 370: SQL Stored Procedures

Stored Procedure Syntax SQL Server


CREATE PROC [ EDURE ] procedure_name [ { @parameter data_type } [ = default ] [ OUTPUT ] ] [ ,...n ] AS sql_statement [ ...n ]

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Example Create a stored proc in SQL-Server


Write this in the SQL Query Analyzer and run to create a new stored procedure:
CREATE PROCEDURE GetStopsOnRoute @RouteId int AS SELECT * FROM StopOnRoute WHERE RouteId=@RouteId

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Same Example, More Detail


CREATE PROCEDURE dbo.GetStopsOnRoute -- returns a chronological listing of bus stops on a given bus route @RouteId int -- PK of a bus route to list

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

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Example: Calling a stored proc

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Modifying Stored Procs in SQL-Server


Use the ALTER PROCEDURE command Instead of CREATE Because the sproc already exists Script stored under Properties Paste into the Query Analyzer Edit, run, debug, run Remember to Refresh in Enterprise Manager Scripts can be stored locally as text files with .sql default extension

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Declaring Local Variables


Syntax: DECLARE {{ @local_variable data_type } } [ ,...n]

Example: DECLARE @StopCount int, @LastName nvarchar(20)

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Assigning Values to Local Variables


Syntax: SET { { @local_variable = expression } Examples: SET @LastName = Manning SET @StopCount = 42 SET @HR = (SELECT Sum(HR) FROM Player)

University of South Alabama School of CIS

Last Modified: 4/13/2012

ITE 370: SQL Stored Procedures

Stored Procs with Output Parameter


ALTER PROC dbo.GetNumOfStopsOnRoute @RouteId int, @StopCount int OUTPUT AS SET @StopCount = (SELECT Count(*) AS Stops FROM StopOnRoute WHERE RouteId=@RouteId)

-- 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

University of South Alabama School of CIS

Last Modified: 4/13/2012

10

You might also like