Top Banner
17
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stored procedures by thanveer danish melayi
Page 2: Stored procedures by thanveer danish melayi

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

Page 3: Stored procedures by thanveer danish melayi

Stored Procedures

Muhammed Thanveer [email protected]

Muhammed Thanveer Danish MelayiMuhammed Thanveer M

typing speed:25

Page 4: Stored procedures by thanveer danish melayi

what is stored procedureA 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

Page 5: Stored procedures by thanveer danish melayi

Click to add text

Page 6: Stored procedures by thanveer danish melayi

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 dataUsers can be granted permission to execute a stored procedure even if they do not have permission to execute the procedure's statements directly.

Page 7: Stored procedures by thanveer danish melayi

Syntax of stored procedure

CREATE PROCEDURE <procedure name>

<Param> <datatype> as

Begin <Body of procedure>

End //call <procedure name>(param

value);

Page 8: Stored procedures by thanveer danish melayi

example for spCreate 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’;

Page 9: Stored procedures by thanveer danish melayi

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.

Page 10: Stored procedures by thanveer danish melayi

example for cursorDeclare 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 below1. DECLARE cursor_name CURSOR2. [LOCAL | GLOBAL] --define cursor scope3. [FORWARD_ONLY | SCROLL] --define cursor movements (forward/backward)4. [STATIC | KEYSET | DYNAMIC | FAST_FORWARD] --basic type of cursor5. [READ_ONLY | SCROLL_LOCKS | OPTIMISTIC] --define locks6. FOR select_statement --define SQL Select statement7. FOR UPDATE [col1,col2,...coln] --define columns that need to be updated

Page 11: Stored procedures by thanveer danish melayi

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

Page 12: Stored procedures by thanveer danish melayi

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

Page 13: Stored procedures by thanveer danish melayi

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

Page 14: Stored procedures by thanveer danish melayi

THANK

YOU

Page 15: Stored procedures by thanveer danish melayi

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.

Page 16: Stored procedures by thanveer danish melayi

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

Page 17: Stored procedures by thanveer danish melayi

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.Email: [email protected]

Contact Us