Stored procedures allow users to save and reuse SQL code. There are three types of stored procedures: user defined, extended, and system stored procedures. User defined stored procedures are custom procedures that perform repetitive tasks. Extended stored procedures allow SQL to interact with the operating system. System stored procedures are used to interact with system tables and are prefixed with "sp_". Stored procedures can take parameters, return results, and include logic like IF/ELSE statements. They improve code reuse and simplify tasks like accessing data across different queries or applications.
2. CONTENTS
Introduction to stored procedures
Types of Stored Procedure
User defined stored procedures
Extended stored procedures
System stored procedures
3. INTRODUCTION TO STORED
PROCEDURES
A stored procedure is nothing more than prepared SQL
code that you save so you can reuse the code over and over
again. So if you think about a query that you write over
and over again, instead of having to write that query each
time you would save it as a stored procedure and then just
call the stored procedure to execute the SQL code that you
saved as part of the stored procedure.
In addition to running the same SQL code over and over
again you also have the ability to pass parameters to the
stored procedure, so depending on what the need is the
stored procedure can act accordingly based on the
parameter values that were passed.
4. User Defined:
User defined stored procedures are also known as
custom stored procedures.These procedures are
used for reusingTransact-SQL statements for
performing repetitive task.
Extended Stored procedures:
Extended stored procedures help SQL in interecting
with operating system.These are carried out in
DLL(Dynamic Link Library).It calls at the runtime.
Types Of Stored Procedures
5. System Stored Procedure
System stored procedure are commonly used
for interacting with system tables.
They are prefixed with ‘sp_’
Catalog Stored Procedure
Database Stored Procedure
Database Engine Stored Procedure
Security Stored Procedure
Full-text Stored Procedure
10. USER DEFINED STORED PROCEDURE
Stored procedures are modules or routines
that encapsulate code for reuse.A stored
procedure can take input parameters, return
tabular or scalar results and messages to the
client, invoke data definition language (DDL)
and data manipulation language (DML)
statements, and return output parameters.
11. Create Stored Procedure
CREATE PROCEDUREGetStudentIdentification
AS
BEGIN
SELECT std_name, father_name,contact_number
FROM students_information
END
GO
Execute Stored Proceedure
exec GetStudentIdentification
12. The code inside the stored procedure can be
something as simple as:
SELECT * FROM USERLIST
Example:
Assume we have a table inventory
This information is updated in real-time and
warehouse managers are constantly checking the
levels of products stored at their warehouse and
available for shipment. In the past, each manager
would run queries similar to the following:
SELECT Product, Quantity FROM Inventory
WHEREWarehouse = 'FL'
13. We can simplify this process through the use
of a stored procedure
CREATE PROCEDURE sp_GetInventory
@location varchar(10)
AS
SELECT Product, Quantity
FROM Inventory
WHEREWarehouse = @location
14. Our Florida warehouse manager can then
access inventory levels by issuing the
command
EXECUTE sp_GetInventory 'FL‘
The NewYork warehouse manager can use
the same stored procedure to access that
area's inventory.
EXECUTE sp_GetInventory 'NY'
15. Using Wildcard Characters:
Create procedure wild_cards
@name varchar(20)
as
Select * from employee where emp_name like @name
execute wild_cards 'D%'
16. Input Variables
There are many reasons for wanting to pass data to a stored
procedure, especially if your stored procedure is being called by a
dynamic web page or other application.
You may want to use a SELECT statement to pull information into
the application for dynamic display. In this case, you would pass
selection criteria to the stored procedure (for use in aWHERE
clause).
If you are inserting new records, you will need to get the data from
somewhere. Updating existing records also involves
simply getting the data. In both INSERT and UPDATE statements,
it is necessary to pass data to the stored procedure. For INSERT,
UPDATE, and SELECT statements (to name a few), you can pass
the data to your stored procedure using variables.
20. CREATE PROCEDURE usp_finduser
@usr_id int
AS
SELECT * FROM USERLIST
WHERE usr_id=@usr_id
Execute Statement:
exec usp_finduser ’1′
21. If – else statement
create procedure insert_details
@emp_id bigint,
@emp_nm varchar(50),
@emp_age bigint,
@emp_sal bigint,
@mgr_id bigint
as
if @emp_id is null
begin
print 'hello'
return
end
else
begin
insert into emp_copy
values(@emp_id,@emp_nm,@emp_age,@emp_sal,@mgr_id)
end
22. TRY…….CATCH Construct
Each try catch construct must be inside a stored procedure.
ATRY block must be immediately followed by a CATCH block.
To handle an error that occurs within a given CATCH block,
write aTRY…CATCH block within the specified CATCH block.
Errors that have the 10 or below severity level are not handled
by try…catch blocks.These errors are considered as warning or
informational messages.
Errors that have severity of 20 or higher are not handled by
try…catch blocks. Database engine close the connection will
not be handled by the try..catch block.
24. EXAMPLE
Create procedure delete_employee
@empid bigint
As
Set @empid=‘E001’
Delete from employee where emp_id=@empid
Begin try
Execute delete_employee
End try
Begin catch
Select error_procedure() as ErrorProcedure
End catch
25. Sp_addmessage stored procedure
Execute sp_addmessage @msgnum=50001,
@severity=10,
@msgtext=‘This is a customized error’,
@lang=‘us_english’
Raiserror(50001,10,1)