module 12 implementing stored procedures
TRANSCRIPT
-
8/9/2019 Module 12 Implementing Stored Procedures
1/34
Module 12:Implementing StoredProcedures
Vidya Vrat Agarwal. | MCT, MCSD
-
8/9/2019 Module 12 Implementing Stored Procedures
2/34
Introduction to Stored Procedures
Creating, Executing, and Modifying Stored Procedures
Using Parameters in Stored Procedures
Executing Extended Stored Procedures
Overview
-
8/9/2019 Module 12 Implementing Stored Procedures
3/34
Introduction of Stored Procedures
A stored procedure is a named collection of Transact-SQL statements thatis stored on the server. Stored procedures are a method ofencapsulating repetitive tasks that executes efficiently.
A precompiled collection of Transact-SQL statements stored under a nameand processed as a unit. SQL Server-supplied stored procedures arecalled system stored procedures.
Named Collections of Transact-SQL Statements
Encapsulate Repetitive Tasks
Five Types (System, Temporary, Local, Extended and Remote)
Accept Input Parameters and Return Values
Return Status Value to Indicate Success or Failure
-
8/9/2019 Module 12 Implementing Stored Procedures
4/34
Initial Processing of Stored Procedures
Entries into sysobjectsand syscomments tables
Entries into sysobjectsand syscomments tables
Compiled plan placed inprocedure cache
Compiled plan placed inprocedure cacheCompilation
Compilation
Optimization
Optimization
Creation
CreationCreation
Execution(first time
or recompile)
ExecutionExecution(first time(first time
or recompile)or recompile)
Parsing
Parsing
-
8/9/2019 Module 12 Implementing Stored Procedures
5/34
Advantages of Stored Procedures
Share Application Logic
Shield Database Schema Details
Provide Security Mechanisms
Improve Performance
Reduce Network Traffic
-
8/9/2019 Module 12 Implementing Stored Procedures
6/34
Creating Stored Procedures
Create in Current Database with the CREATEPROCEDURE Statement
Can Nest to 32 Levels
Use sp_help to Display Information
Use library
GO
CREATE PROC[EDURE] overdue_books
AS
SELECT *
FROM dbo.loan
WHERE due_date < GETDATE()
GO
EXEC overdue_books
-
8/9/2019 Module 12 Implementing Stored Procedures
7/34
Guidelines for Creating Stored Procedures
dbo User Should Own All Stored Procedures
One Stored Procedure for One Task
Create, Test, and Debug on Server
Avoid sp_ Prefix in Stored Procedure Names
Use Same Connection Settings for All Stored Procedures
Minimize Use of Temporary Stored Procedures
-
8/9/2019 Module 12 Implementing Stored Procedures
8/34
Input parameters allow information to be passed into astored procedure. To define a stored procedure that acceptsinput parameters, you declare one or more variables asparameters in the CREATE PROCEDURE statement.
The maximum number of parameters in a stored procedureis 1024.
Parameters are local to a stored procedure. The sameparameter names can be used in other stored procedures.
-
8/9/2019 Module 12 Implementing Stored Procedures
9/34
Using Input Parameters
Create Proc Pname@myname varchar(20) = Vidyaasprint 'My Name is' + ' ' + @myname
Exec Procedure Name [Parameter]
Exec pname Vidya My Name is Vidya
Exec pname Vidya Vrat My Name is Vidya Vrat
Exec pname My Name is Vidya
Pname My Name is Vidya
Step 1
Step 2
-
8/9/2019 Module 12 Implementing Stored Procedures
10/34
create proc pst
@sid as char(8)
as
select * from student
where student_id=@sid
exec pst '1111111'
1111111 Antonius Purba Male 20 Medan
-
8/9/2019 Module 12 Implementing Stored Procedures
11/34
Executing Stored Procedures with Input Parameters
Passing Values by Reference
Passing Values by Position
EXEC addadult 'LaBrie', 'Linda', null,
'Dogwood Drive', 'Sacramento', 'CA',
'94203', null
EXEC addadult 'LaBrie', 'Linda', null,
'Dogwood Drive', 'Sacramento', 'CA',
'94203', null
EXEC addadult
@firstname = 'Linda',
@lastname = 'LaBrie',
@street = 'Dogwood Drive',
@city = 'Sacramento',@state = 'CA',
@zip = '94203'
EXEC addadult
@firstname = 'Linda',
@lastname = 'LaBrie',
@street = 'Dogwood Drive',
@city = 'Sacramento',
@state = 'CA',
@zip = '94203'
-
8/9/2019 Module 12 Implementing Stored Procedures
12/34
Updating Data
UPDATE statement
NOCOUNT option
CREATE PROCEDURE p_UpdateCategory(
@CategoryID int = null,
@CategoryName varchar(50)
)
AS
SET NOCOUNT ONUPDATE Categories
SET Category = @CategoryName
WHERE CategoryID = @CategoryID
CREATE PROCEDURE p_UpdateCategory
(
@CategoryID int = null,
@CategoryName varchar(50)
)
AS
SET NOCOUNT ONUPDATE Categories
SET Category = @CategoryName
WHERE CategoryID = @CategoryID
-
8/9/2019 Module 12 Implementing Stored Procedures
13/34
Inserting Data
INSERT Statement
CREATE PROCEDURE p_InsertCustomer(
@FName varchar(50),
@LName varchar(50)
)
AS
SET NOCOUNT ON
INSERT INTO Customers (FirstName, LastName)
VALUES (@FName, @LName)
CREATE PROCEDURE p_InsertCustomer(
@FName varchar(50),
@LName varchar(50)
)
AS
SET NOCOUNT ONINSERT INTO Customers (FirstName, LastName)
VALUES (@FName, @LName)
-
8/9/2019 Module 12 Implementing Stored Procedures
14/34
Deleting Data
DELETE Statement
CREATE PROCEDURE p_DeleteCategory
(@CategoryID int = null
)
AS
SET NOCOUNT ON
DELETE FROM Categories
WHERE CategoryID = @CategoryID
CREATE PROCEDURE p_DeleteCategory
(
@CategoryID int = null
)
AS
SET NOCOUNT ON
DELETE FROM Categories
WHERE CategoryID = @CategoryID
-
8/9/2019 Module 12 Implementing Stored Procedures
15/34
Returning Values with Output Parameters
CREATE PROCEDURE mathtutor
@m1 smallint,
@m2 smallint,
@result smallint OUTPUT
AS
SET @result = @m1 * @m2
DECLARE @answer smallint
EXECUTE mathtutor 5, 6, @answer OUTPUT
SELECT 'The result is: ' , @answer
The result is: 30
CREATE PROCEDURE mathtutor
@m1 smallint,
@m2 smallint,
@result smallint OUTPUT
AS
SET @result = @m1 * @m2
DECLARE @answer smallint
EXECUTE mathtutor 5, 6, @answer OUTPUT
SELECT 'The result is: ' , @answer
The result is: 30
Results of StoredProcedure
Results of StoredResults of StoredProcedureProcedure
Executing StoredProcedure
Executing StoredExecuting StoredProcedureProcedure
Creating StoredProcedure
Creating StoredCreating StoredProcedureProcedure
-
8/9/2019 Module 12 Implementing Stored Procedures
16/34
OUTPUT Parameter
Stored procedures can return information to the calling storedprocedure or client with output parameters (variablesdesignated with the OUTPUT keyword).
By using output parameters, any changes to the parameterthat result from the execution of the stored procedure can be
retained, even after the stored procedure completesexecution.
To use an output parameter, the OUTPUT keyword must bespecified in both the CREATE PROCEDURE and EXECUTEstatements.
If the keyword OUTPUT is omitted when the stored procedureis executed, the stored procedure still executes, but it doesnot return a value. i.e. Shows NULL .
-
8/9/2019 Module 12 Implementing Stored Procedures
17/34
Executing Extended Stored Procedures
Increase SQL Server Functionality
Are Programmed Using Open Data Services API
Can Include C and C++ Features Can Be Added to the master Database Only
EXEC master..xp_cmdshell 'dir c:\mssql7'
-
8/9/2019 Module 12 Implementing Stored Procedures
18/34
Return Statement
The RETURN statement exits from a query or storedprocedure unconditionally. It also can return an integerstatus value (return code).
A return value of 0 indicates success; 1-99 indicates error
-
8/9/2019 Module 12 Implementing Stored Procedures
19/34
Return statement
create proc ps
@age int
as
if (@age > 28) return (1)
select * from student
where age >=@age
return(0)
declare @status int
exec @status= ps 20
select @status
-
8/9/2019 Module 12 Implementing Stored Procedures
20/34
Check Your Understanding.
-
8/9/2019 Module 12 Implementing Stored Procedures
21/34
Q.1 What is Stored Procedure.?
-
8/9/2019 Module 12 Implementing Stored Procedures
22/34
Q.2. How many types of stored procedures are availablewith SQL server. ?
-
8/9/2019 Module 12 Implementing Stored Procedures
23/34
Q.3. You should use sp_ Prefix in Stored ProcedureNames.?
1. Yes
2. No
-
8/9/2019 Module 12 Implementing Stored Procedures
24/34
Q.4. Return Value, Indicates Success or Failure.?
1. True
2. False
-
8/9/2019 Module 12 Implementing Stored Procedures
25/34
Q.5. The maximum number of parameters in a storedprocedure is _____________ .
-
8/9/2019 Module 12 Implementing Stored Procedures
26/34
Q.6. What will be the output of the following :
EXEC master..xp_cmdshell 'dir c:\mssql7
-
8/9/2019 Module 12 Implementing Stored Procedures
27/34
Q.7. Name the five stored procedures availablewith SQL Server.
-
8/9/2019 Module 12 Implementing Stored Procedures
28/34
Q.8. What are the Mistakes in the given Create Proccommand.
cREATE pROC Pnamemyname varchar(20) = PIDelasprint 'My Name is' + ' ' + @myname
-
8/9/2019 Module 12 Implementing Stored Procedures
29/34
Q.9. The same parameter names can be used inother stored procedures.
1. True
2. False
-
8/9/2019 Module 12 Implementing Stored Procedures
30/34
Q.10. If the keyword OUTPUT is omitted when the storedprocedure is executed, the stored procedure still executes,but it does not return a value.
1. True
2. False
-
8/9/2019 Module 12 Implementing Stored Procedures
31/34
Q.11. A junior SQL Server programmer asks you for help. Sheexecuted a SQL statement on the server from her computer.Before she could verify that it executed properly, she lost networkconnectivity. From your computer, you access the server and usethe @@error system function. It returns a value of 0. What doesthat indicate about the last statement executed on the server?
1. It stopped executing due to the loss of connectivity.
2. It logged an error number 0.
3. It logged an error with a severity level of 0.
4. It is executed successfully.
-
8/9/2019 Module 12 Implementing Stored Procedures
32/34
Q.12. You are the system administrator of a productiondatabase server. You want to create a storedprocedure to automate certain administrative tasks on
the Finance database. You do not want the storedprocedure to be used in other databases. What type ofstored procedure should you use?
1. System
2. Local3. Global temporary
4. Extended.
-
8/9/2019 Module 12 Implementing Stored Procedures
33/34
Review
Introduction to Stored Procedures
Creating, Executing, and Modifying Stored Procedures
Using Parameters in Stored Procedures
Executing Extended Stored Procedures
-
8/9/2019 Module 12 Implementing Stored Procedures
34/34
It is not enoughto aimyou mustHit .
Thank You.