Chapter 3 stored procedures
-
Upload
baabtracom-no-1-supplier-of-quality-freshers -
Category
Technology
-
view
217 -
download
0
Transcript of Chapter 3 stored procedures
![Page 1: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/1.jpg)
Introduction To DBMS and SQL Server
Stored Procedures
![Page 2: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/2.jpg)
Stored Procedures
• A stored procedure is a method to encapsulate repetitive tasks.
• A stored Procedure is actually stored in database data dictionary
![Page 3: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/3.jpg)
Without Stored Procedure
Employee.jsp......................
Select * from tbl_employee ................
Report.php..........................
Select * from tbl_employee .................
.....................Select * from tbl_employee
viewDetails.php...................................................
Select * from tbl_employee
...............
Database
Select * from tbl_employee
Select * from tbl_employee
Select * from tbl_employee
![Page 4: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/4.jpg)
Stored Procedure
Employee.php....................................................
exec getName();................
Report.php.............................................................................
exec getName();
viewDetails.php...................................................
exec getName();...............
getName()BeginSelect * from tbl_employee End
Database
![Page 5: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/5.jpg)
Advantages• Precompiled Execution
• sqlserver compiles each stored procedure once and then re utilizes the execution plan. This result in tremendous performance boosts when stored procedures are called repeatedly
• Reduced client server traffic• If network traffic is concerned you will be happy to
learn that stored procedures can reduce long sql queries to a single line that is transmitted between application program and database server.
![Page 6: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/6.jpg)
Advantages• Efficient reuse of code and programming abstraction
•Stored procedures can be used by multiple programs and users. If you utilize them in a planned manner, you’ll find the development cycle takes less time.
• Enhanced Security Control•You can grant users permission to execute stored
procedure independently of underlying table permissions
![Page 7: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/7.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getName(@id int)as
BEGINSelect * from tbl_user where userid=@id;
END
EXEC getName @id=1 ;
![Page 8: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/8.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getName(@id int)
BEGIN
Select * from tbl_user where userid=@id;END
EXEC getName @id=1;
Is the keyword to create a new stored procedure .we can use proc also
![Page 9: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/9.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getName(@id int)
BEGINSelect * from tbl_user where userid=@id;
END
EXEC getName @id=1
Is the name of the stored procedure that we are creating
![Page 10: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/10.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getName ( @id int)BEGIN
Select * from tbl_user where userid=@id;END
Call getName @id=1;
Is the body of the stored procedure. Here we have only a single select query statements. We can also apply logic using the below • DECLARE a INT; : declaring an integer
type variable
• SET a=20; : Setting value of a to 20
• IF THEN• ELSE IF
label1: LOOP SET p1 = p1 + 1; IF p1 < 10 THEN ITERATE label1; END IF; LEAVE label1; END LOOP label1;
: For conditions
: Loops
![Page 11: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/11.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getName ( @id int)BEGIN
Select * from tbl_user where userid=@id;END
exec getName=@id=1;
![Page 12: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/12.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getName ( @id int)BEGIN
Select * from tbl_user where userid=@id;END
exec getName @id=1;
Calling the stored procedure we’ve just created and passing the value 1001 as its argument
![Page 13: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/13.jpg)
How to create Stored Procedure ? example
CREATE PROCEDURE getSale1(@id int,@pri int output ,@sal int output)as BEGIN
Select @pri=sum(price), @sal=avg(sales) from tbl_sales where pk_int_id=@id;
ENDdeclare @total intdeclare @sale intexec getSale1 @id=1,@pri=@total output,@sal=@sale output;select @total; select @sale;
![Page 14: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/14.jpg)
• Create the below table
Create a stored procedure called
– csp_getSalary(1000) : should return the salary of employee with id as passed in the argument
– Csp_getSalaryAtPlace(‘calicut’,@total) : should return the total salary of employees from a perticular place
Live Task
Tbl_employeeEmp_id
Emp_name
Emp_age
Emp_email int_salary vchr_place
1000 Deepak 24 [email protected]
10000 Calicut
1001 Aneesh 23 [email protected]
20000 Cochin
1002 Naveen 25 [email protected]
10000 Calicut
1003 Jacob 25 [email protected]
30000 Cochin
![Page 15: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/15.jpg)
Cursors
• SELECT INTO is fine for single-row queries, but many applications require the querying of multiple rows of data. You will use a cursor in SQL Server to accomplish this.
• A cursor lets you fetch one or more rows from a SQL result set into stored program variables, usually with the intention of performing some row-by-row processing on the result set.
![Page 16: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/16.jpg)
Cursors - examplecreate procedure CSP_status_changeas begindeclare @done int;set @done=0;declare @studId int;declare @studStatus bit;declare @dateDif int;declare @myCur cursorset @myCur = cursor for select pk_int_student_id,bln_student_status,datediff(day,dat_fph_date,GETDATE())as date_difference from
tbl_students join tbl_fee_payment_history on
fk_int_student_id=pk_int_student_id;open @myCur
![Page 17: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/17.jpg)
fetch next from @myCur into @studId,@studStatus,@dateDif;while @@FETCH_STATUS = 0 begin if @dateDif>15
update tbl_students set bln_student_status=0 where pk_int_student_id=@studId;else
update tbl_students set bln_student_status=1 where pk_int_student_id=@studId;
fetch next from @myCur into @studId,@studStatus,@dateDif;end;
close @myCur;deallocate @myCur;END;exec CSP_status_change;fetch next from @myCur
![Page 18: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/18.jpg)
Questions?“A good question deserve a good
grade…”
![Page 19: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/19.jpg)
Self Check !!
![Page 20: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/20.jpg)
• Why should someone use stored procedure?
– To avoid data redundancy– To reduce network traffic between application
server and database server– To ensure data integrity
Self Check !!
![Page 21: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/21.jpg)
• Why should someone use stored procedure?
– To avoid data redundancy– To reduce network traffic between application
server and database server– To ensure data integrity
Self Check !!
![Page 22: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/22.jpg)
Self Check !!
• Why should someone change the delimiter before creating a stored procedure?
– To use ; as separation between multiple statements in a stored procedure body
– To push the server to compile the whole body of stored procedure all together
– Both of above– None of above
![Page 23: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/23.jpg)
Self Check !!
• Why should someone change the delimiter before creating a stored procedure?
– To use ; as separation between multiple statements in a stored procedure body
– To push the server to compile the whole body of stored procedure all together
– Both of above– None of above
![Page 24: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/24.jpg)
Self Check !!
CREATE PROCEDURE simpleProc ( OUT param1 INT)BEGIN SELECT COUNT(*) INTO param1 FROM t ;END
EXEC simpleProc(@a);Select @a ;
![Page 25: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/25.jpg)
Self Check !!
CREATE PROCEDURE simpleProc ( @param1 INT)BEGIN SELECT COUNT(*) INTO param1 FROM t ;END
exec simpleProc(@a);Select @a ;
![Page 26: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/26.jpg)
Self Check !!
What are the uses of cursors?
•For extracting multiple rows from a table•For extracting multiple rows into variables from a table•For setting handlers
![Page 27: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/27.jpg)
Self Check !!
What are the uses of cursors?
•For extracting multiple rows from a table•For extracting multiple rows into variables from a table•For setting handlers
![Page 28: Chapter 3 stored procedures](https://reader034.fdocuments.in/reader034/viewer/2022051113/55b5ae4dbb61eb8c7d8b4576/html5/thumbnails/28.jpg)
End of day 1