SlideServe
Menu
Préférences
© SlideServe · v2026

SQL Stored Procedures

azizi avatar
12 diapositives · 1.8K Views · Jun 28, 2012

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

SQL Stored Procedures - slide 1
01 / 12SQL Stored Procedures
1 / 17

Télécharger la présentation

SQL Stored Procedures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. SQL Stored Procedures Jeffrey P. Landry University of South Alabama University of South Alabama School of CIS

  2. 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 SQL-Server University of South Alabama School of CIS

  3. Advantages of Stored Procedures • Reuse • write once, call many • can be shared by multiple users of db • Convenient means of abstraction • Easier to write than SQL embedded within VB • Centralizes access to data • More secure means of data access • Fast execution • precompiled SQL statements • cached results University of South Alabama School of CIS

  4. Disadvantages of Stored Procedures • Extra layer of hidden complexity on top of SQL statements • Can be used to restrict application and user access to data • Programming environment not as advanced as modern, visual IDEs • Fewer advanced code writing, testing, debugging features available in SQL-Server • No search and replace University of South Alabama School of CIS

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

  6. 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 GetEmployeesInDept @Dept int AS SELECT * FROM Employee WHERE DeptId=@Dept University of South Alabama School of CIS

  7. Stored Proc Example with Insert CREATE PROCEDURE dbo.InsertEmployee -- adds a new row to the table of employees @First varchar(15), -- employee's first name @Last nvarchar(15), -- employee's last name @Hire DateTime, -- date that employee was hired by the company @PayRate decimal(8,2) -- employee's hourly rate of pay AS BEGIN -- sproc INSERT INTO Employee (FirstName, LastName, HireDate, PayRate) VALUES (@First, @Last, @Hire, @PayRate) END -- sproc -- example call to insert a new employee row -- EXEC dbo.InsertEmployee 'Kelly', 'Reed', '4/8/2005', 17.50 -- verify that row was added by searching for it -- SELECT * FROM Employee WHERE LastName LIKE 'Re%' -- note the % wild-card -- execute the script GO University of South Alabama School of CIS

  8. Example: Calling a stored proc University of South Alabama School of CIS

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

  10. Declaring Local Variables • Syntax: DECLARE     {{ @local_variable data_type }             } [ ,...n] • Example: DECLARE @ManagerId int, @LastName nvarchar(15) University of South Alabama School of CIS

  11. Assigning Values to Local Variables • Syntax: SET { { @local_variable = expression } • Examples: SET @LastName = ‘Manning’ SET @ManagerId = 42 SET @AvgPay = (SELECT Avg(PayRate) FROM Employee) University of South Alabama School of CIS

  12. Stored Procs with Output Parameter CREATE PROC dbo.GetNumOfEmployeesInDept @DeptId int, @EmployeeCount int OUTPUT AS SET @EmployeeCount = (SELECT Count(*) AS EmpCnt FROM Employee WHERE DeptId=@DeptId) -- example call to retrieve number of employees in DeptId=1 /* DECLARE @NumOfEmps int SET @NumOfEmps = 0 EXEC dbo.GetNumOfEmployeesInDept 1, @EmployeeCount = @NumOfEmps OUTPUT SELECT @NumOfEmps AS TotalEmps */ GO University of South Alabama School of CIS