Module 12 Implementing Stored Procedures
Module 12 Implementing Stored Procedures
Overview
Introduction to Stored Procedures Creating, Executing, and Modifying Stored Procedures Using Parameters in Stored Procedures Executing Extended 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
Parsing Parsing
Execution Execution
(first (firsttime time or orrecompile) recompile)
Optimization Optimization
Compilation Compilation
Compiled Compiled plan plan placed placed in in procedure procedure cache cache
Share Application Logic Shield Database Schema Details Provide Security Mechanisms Improve Performance Reduce Network Traffic
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
Input parameters allow information to be passed into a stored procedure. To define a stored procedure that accepts input parameters, you declare one or more variables as parameters in the CREATE PROCEDURE statement. The maximum number of parameters in a stored procedure is 1024. Parameters are local to a stored procedure. The same parameter names can be used in other stored procedures.
Step 1
Step 2
create proc pst @sid as char(8) as select * from student where student_id=@sid exec pst '1111111' 1111111 Antonius Purba Male 20 Medan
Updating Data
CREATE CREATE PROCEDURE PROCEDURE p_UpdateCategory p_UpdateCategory ( ( @CategoryID @CategoryID int int = = null, null, @CategoryName @CategoryName varchar(50) varchar(50) ) ) AS AS SET SET NOCOUNT NOCOUNT ON ON UPDATE UPDATE Categories Categories SET SET Category Category = = @CategoryName @CategoryName WHERE WHERE CategoryID CategoryID = = @CategoryID @CategoryID
Inserting Data
INSERT Statement
CREATE CREATE PROCEDURE PROCEDURE p_InsertCustomer p_InsertCustomer ( ( @FName @FName varchar(50), varchar(50), @LName @LName varchar(50) varchar(50) ) ) AS AS SET SET NOCOUNT NOCOUNT ON ON INSERT INSERT INTO INTO Customers Customers (FirstName, (FirstName, LastName) LastName) VALUES VALUES (@FName, (@FName, @LName) @LName)
Deleting Data
DELETE Statement
CREATE CREATE PROCEDURE PROCEDURE p_DeleteCategory p_DeleteCategory ( ( @CategoryID @CategoryID int int = = null null ) ) AS AS SET SET NOCOUNT NOCOUNT ON ON DELETE DELETE FROM FROM Categories Categories WHERE WHERE CategoryID CategoryID = = @CategoryID @CategoryID
OUTPUT Parameter
Stored procedures can return information to the calling stored procedure or client with output parameters (variables designated with the OUTPUT keyword). By using output parameters, any changes to the parameter that result from the execution of the stored procedure can be retained, even after the stored procedure completes execution. To use an output parameter, the OUTPUT keyword must be specified in both the CREATE PROCEDURE and EXECUTE statements. If the keyword OUTPUT is omitted when the stored procedure is executed, the stored procedure still executes, but it does not return a value. i.e. Shows NULL .
Return Statement
The RETURN statement exits from a query or stored procedure unconditionally. It also can return an integer status value (return code). A return value of 0 indicates success; 1-99 indicates error
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
Q.2. How many types of stored procedures are available with SQL server. ?
Yes No
1. 2.
Q.6. What will be the output of the following : EXEC master..xp_cmdshell 'dir c:\mssql7
Q.7. Name the five stored procedures available with SQL Server.
Q.8. What are the Mistakes in the given Create Proc command. cREATE pROC Pname myname varchar(20) = PIDel as print 'My Name is' + ' ' + @myname
Q.9. The same parameter names can be used in other stored procedures.
True False
1. 2.
procedure is executed, the stored procedure still executes, but it does not return a value.
1. 2.
True False
Q.11. A junior SQL Server programmer asks you for help. She
executed a SQL statement on the server from her computer. Before she could verify that it executed properly, she lost network connectivity. From your computer, you access the server and use the @@error system function. It returns a value of 0. What does that indicate about the last statement executed on the server? It stopped executing due to the loss of connectivity. It logged an error number 0. It logged an error with a severity level of 0. It is executed successfully.
1. 2. 3. 4.
Q.12. You are the system administrator of a production database server. You want to create a stored procedure to automate certain administrative tasks on the Finance database. You do not want the stored procedure to be used in other databases. What type of stored procedure should you use?
1. 2. 3. 4.
Review
Introduction to Stored Procedures Creating, Executing, and Modifying Stored Procedures Using Parameters in Stored Procedures Executing Extended Stored Procedures