The document discusses stored procedures and cursors in SQL. It defines stored procedures as reusable blocks of SQL code that can be called multiple times with different input parameters to perform repetitive tasks. Cursors allow row-by-row processing of result sets. The document provides examples of creating stored procedures and using cursors to iterate through rows retrieved from a database table. It also lists advantages of stored procedures like modular programming, faster execution, reduced network traffic, and better security.
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
Stored procedures by thanveer danish melayi
1.
2. Disclaimer: This presentation is prepared by trainees of
baabtra.com as a part of mentoring program. This is not
official document of baabtra.com – Mentoring Partner
3. Stored Procedures
Muhammed Thanveer Melayi
shernoobi@gmail.com
Muhammed Thanveer Danish Melayi
Muhammed Thanveer M
typing speed:25
4. what is stored procedure
A stored procedure is a group of sql statements that has been
created and stored in the database.
Stored procedure will accept input parameters so that a single
procedure can be used over the network by several clients using
different input data.
Stored procedure will reduce network traffic and increase the
performance
6. Advantages of using stored procedures
a) Stored procedure allows modular programming.
You can create the procedure once, store it in the database, and call it any
number of times in your program.
b) Stored Procedure allows faster execution.
if the operation requires a large amount of SQL code is performed
repetitively, stored procedures can be faster.
c) Stored Procedure can reduce network traffic.
An operation requiring hundreds of lines of Transact-SQL code can be
performed through a single statement that executes the code in a procedure,
rather than by sending hundreds of lines of code over the network.
d) Stored procedures provide better security to your data
Users can be granted permission to execute a stored procedure even if they
do not have permission to execute the procedure's statements directly.
7. Syntax of stored procedure
CREATE PROCEDURE <procedure
name>
<Param> <datatype>
as
Begin
<Body of procedure>
End
//call <procedure name>(param
value);
8. example for sp
Create table tbl_login
(
Vchr_name varchar(50),
V chr_userid varchar(50)
);
Create table tbl_user(
Vchr_user_name varchar(50),
Vchr_password varchar(50)
);
Create procedure insert_value
(
@name varchar(50),
@userid varchar(50),
@username varchar(50),
@password varchar(50)
)
As
Begin
Insert into tbl_login,tbl_user(vchr_name,vchr_user_id,vchr_username,vchr_password)
values(@name,@userid,@username,@password)
end
exec insert_ value ‘ram’,’102’,’akhil’,’12345’;
9. cursors
Cursor is a variable in SQL Server Database which is used for
row-by row operations. The cursor is so named because it
indicates the current position in the resultset.
Use of cursor
We use cursor when we need to update records in a database table
in singleton fashion means row by row.
10. example for cursor
Declare Cursor SQL Comand is used to define the cursor with many options
that impact the scalablity and loading behaviour of the cursor. The basic
syntax is given below
1. DECLARE cursor_name CURSOR
2. [LOCAL | GLOBAL] --define cursor scope
3. [FORWARD_ONLY | SCROLL] --define cursor movements (forward/backward)
4. [STATIC | KEYSET | DYNAMIC | FAST_FORWARD] --basic type of cursor
5. [READ_ONLY | SCROLL_LOCKS | OPTIMISTIC] --define locks
6. FOR select_statement --define SQL Select statement
7. FOR UPDATE [col1,col2,...coln] --define columns that need to be updated
11. example
CREATE PROCEDURE Usp_cursor_test
AS
BEGIN
–Declaring the variables needed for cursor to
store data
DECLARE @Name VARCHAR(50)
DECLARE @EmptypeID INT
–Declaring the Cursor cur_print For name and
Emptypeid in the Employeedetails table
DECLARE cur_print CURSOR FOR
SELECT name,
emptypeid
FROM employee.employeedetails
–After declaring we have to open the cursor
OPEN cur_print
–retreives the First row from cursor and storing
it into the variables.
12. FETCH NEXT FROM cur_print INTO @Name, @EmptypeID
– @@FETCH_STATUS returns the status of the last
cursor FETCH statement issued against
– any cursor currently opened by the
connection.
– @@FETCH_STATUS = 0 means The FETCH statement
was successful.
– @FETCH_STATUS = -1 The FETCH statement failed
or the row was beyond the result set.
– @@FETCH_STATUS = -2 The row fetched is
missing.
WHILE @@FETCH_STATUS = 0
BEGIN
13. Operations need to be done,Here just printing the variables
PRINT @Name
PRINT @EmptypeID
–retreives the NExt row from cursor and storing
it into the variables.
FETCH NEXT FROM cur_print INTO @Name,
@EmptypeID
END
–Closing the cursor
CLOSE cur_print
– removes the cursor reference and relase cursor from
memory
– very Important
DEALLOCATE cur_print
END
15. Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
16. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
17. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com