SlideShare a Scribd company logo
Introduction To
DBMS and SQL Server
Stored Procedures
Stored Procedures
• A stored procedure is a method to encapsulate
repetitive tasks.
• A stored Procedure is actually stored in
database data dictionary
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
Stored Procedure
Employee.php
..........................
..........................
exec
getName();
................
Report.php
..........................
.........................
..........................
exec
getName();
viewDetails.php
..........................
.........................
exec
getName();
...............
getName()
Begin
Select * from tbl_employee
End
Database
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.
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
How to create Stored Procedure ?
example
CREATE PROCEDURE getName(@id int)
as
BEGIN
Select * from tbl_user where userid=@id;
END
EXEC getName @id=1 ;
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
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 name of the stored
procedure that we are creating
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
How to create Stored Procedure ?
example
CREATE PROCEDURE getName ( @id int)
BEGIN
Select * from tbl_user where userid=@id;
END
exec getName=@id=1;
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
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;
END
declare @total int
declare @sale int
exec getSale1 @id=1,@pri=@total output,@sal=@sale output;
select @total; select @sale;
• 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_employee
Emp_id Emp_name Emp_age Emp_email int_salary vchr_place
1000 Deepak 24 dk@gmail.com 10000 Calicut
1001 Aneesh 23 an@gmail.com 20000 Cochin
1002 Naveen 25 nn@gmail.com 10000 Calicut
1003 Jacob 25 jb@gmail.com 30000 Cochin
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.
Cursors - example
create procedure CSP_status_change
as begin
declare @done int;
set @done=0;
declare @studId int;
declare @studStatus bit;
declare @dateDif int;
declare @myCur cursor
set @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
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
Questions?
“A good question deserve a good
grade…”
Self Check !!
• 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 !!
• 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 !!
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
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
Self Check !!
CREATE PROCEDURE simpleProc ( OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t ;
END
EXEC simpleProc(@a);
Select @a ;
Self Check !!
CREATE PROCEDURE simpleProc ( @param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t ;
END
exec simpleProc(@a);
Select @a ;
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
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
End of day 1

More Related Content

What's hot

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
Pooja Dixit
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
Dhananjay Goel
 
Postgresql
PostgresqlPostgresql
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
Arun Sial
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
Lakshman Basnet
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
Edureka!
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
SQL
SQLSQL
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
webhostingguy
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
Prof.Nilesh Magar
 
DDL And DML
DDL And DMLDDL And DML
DDL And DML
pnp @in
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
PLSQL
PLSQLPLSQL
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
WebStackAcademy
 
Sql commands
Sql commandsSql commands

What's hot (20)

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Postgresql
PostgresqlPostgresql
Postgresql
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
SQL
SQLSQL
SQL
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
DDL And DML
DDL And DMLDDL And DML
DDL And DML
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
PLSQL
PLSQLPLSQL
PLSQL
 
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
 
Sql commands
Sql commandsSql commands
Sql commands
 

Viewers also liked

Delay analysis final
Delay analysis finalDelay analysis final
Delay analysis final
Mahmoud Awwad, PMP, MCIOB
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
DataminingTools Inc
 
Sql Server 2012
Sql Server 2012Sql Server 2012
Sql Server 2012
Performics.Convonix
 
MS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTUREMS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTURE
Douglas Bernardini
 
OLAP
OLAPOLAP

Viewers also liked (6)

Delay analysis final
Delay analysis finalDelay analysis final
Delay analysis final
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Sql Server 2012
Sql Server 2012Sql Server 2012
Sql Server 2012
 
MS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTUREMS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTURE
 
OLAP
OLAPOLAP
OLAP
 

Similar to Chapter 3 stored procedures

Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
ftz 420
 
Sql transacation
Sql transacationSql transacation
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure Presentation
Amin Uddin
 
Stored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh SinghStored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh Singh
imdurgesh
 
Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?
Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?
Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?
Jim Czuprynski
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
devObjective
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
ColdFusionConference
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Stored procedure,transaction
Stored procedure,transactionStored procedure,transaction
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
Alex Zaballa
 
My Database Skills Killed the Server
My Database Skills Killed the ServerMy Database Skills Killed the Server
My Database Skills Killed the Server
ColdFusionConference
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
Muhammed Thanveer M
 
Msql
Msql Msql
Msql
ksujitha
 
Database training for developers
Database training for developersDatabase training for developers
Database training for developers
Bhaveshkumar Thakkar
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
yazidds2
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
Module04
Module04Module04
Module04
Sridhar P
 
Change tracking
Change trackingChange tracking
Change tracking
Sonny56
 

Similar to Chapter 3 stored procedures (20)

Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Sql transacation
Sql transacationSql transacation
Sql transacation
 
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure Presentation
 
Stored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh SinghStored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh Singh
 
Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?
Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?
Autonomous Transaction Processing (ATP): In Heavy Traffic, Why Drive Stick?
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
Stored procedure,transaction
Stored procedure,transactionStored procedure,transaction
Stored procedure,transaction
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
My Database Skills Killed the Server
My Database Skills Killed the ServerMy Database Skills Killed the Server
My Database Skills Killed the Server
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
 
Msql
Msql Msql
Msql
 
Database training for developers
Database training for developersDatabase training for developers
Database training for developers
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Module04
Module04Module04
Module04
 
Change tracking
Change trackingChange tracking
Change tracking
 

More from baabtra.com - No. 1 supplier of quality freshers

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Best coding practices
Best coding practicesBest coding practices
Core java - baabtra
Core java - baabtraCore java - baabtra
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php database connectivity
Php database connectivityPhp database connectivity
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Blue brain
Blue brainBlue brain
5g
5g5g
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Gd baabtra
Gd baabtraGd baabtra
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Cell phone jammer
Cell phone jammerCell phone jammer

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 
Cell phone jammer
Cell phone jammerCell phone jammer
Cell phone jammer
 

Recently uploaded

"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
Fwdays
 
Enterprise_Mobile_Security_Forum_2013.pdf
Enterprise_Mobile_Security_Forum_2013.pdfEnterprise_Mobile_Security_Forum_2013.pdf
Enterprise_Mobile_Security_Forum_2013.pdf
Yury Chemerkin
 
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Zilliz
 
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptxFIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Alliance
 
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
Fwdays
 
Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024
Peter Caitens
 
Top 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdfTop 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdf
Marrie Morris
 
FIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptxFIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptx
FIDO Alliance
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
Zilliz
 
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Alliance
 
History and Introduction for Generative AI ( GenAI )
History and Introduction for Generative AI ( GenAI )History and Introduction for Generative AI ( GenAI )
History and Introduction for Generative AI ( GenAI )
Badri_Bady
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
siddu769252
 
Indian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for StartupsIndian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for Startups
AMol NAik
 
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptxFIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Alliance
 
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI CertificationTrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc
 
Redefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI CapabilitiesRedefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI Capabilities
Priyanka Aash
 
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptxFIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Alliance
 
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptxFIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Alliance
 
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise ExcellenceCracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Quentin Reul
 
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and ConsiderationsChoosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
webbyacad software
 

Recently uploaded (20)

"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
 
Enterprise_Mobile_Security_Forum_2013.pdf
Enterprise_Mobile_Security_Forum_2013.pdfEnterprise_Mobile_Security_Forum_2013.pdf
Enterprise_Mobile_Security_Forum_2013.pdf
 
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
 
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptxFIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptx
 
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
 
Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024
 
Top 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdfTop 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdf
 
FIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptxFIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptx
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
 
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
 
History and Introduction for Generative AI ( GenAI )
History and Introduction for Generative AI ( GenAI )History and Introduction for Generative AI ( GenAI )
History and Introduction for Generative AI ( GenAI )
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
 
Indian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for StartupsIndian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for Startups
 
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptxFIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptx
 
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI CertificationTrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
 
Redefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI CapabilitiesRedefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI Capabilities
 
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptxFIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
 
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptxFIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptx
 
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise ExcellenceCracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
 
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and ConsiderationsChoosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
 

Chapter 3 stored procedures

  • 1. Introduction To DBMS and SQL Server Stored Procedures
  • 2. Stored Procedures • A stored procedure is a method to encapsulate repetitive tasks. • A stored Procedure is actually stored in database data dictionary
  • 3. 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
  • 5. 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.
  • 6. 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
  • 7. How to create Stored Procedure ? example CREATE PROCEDURE getName(@id int) as BEGIN Select * from tbl_user where userid=@id; END EXEC getName @id=1 ;
  • 8. 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
  • 9. 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 name of the stored procedure that we are creating
  • 10. 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
  • 11. How to create Stored Procedure ? example CREATE PROCEDURE getName ( @id int) BEGIN Select * from tbl_user where userid=@id; END exec getName=@id=1;
  • 12. 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
  • 13. 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; END declare @total int declare @sale int exec getSale1 @id=1,@pri=@total output,@sal=@sale output; select @total; select @sale;
  • 14. • 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_employee Emp_id Emp_name Emp_age Emp_email int_salary vchr_place 1000 Deepak 24 dk@gmail.com 10000 Calicut 1001 Aneesh 23 an@gmail.com 20000 Cochin 1002 Naveen 25 nn@gmail.com 10000 Calicut 1003 Jacob 25 jb@gmail.com 30000 Cochin
  • 15. 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.
  • 16. Cursors - example create procedure CSP_status_change as begin declare @done int; set @done=0; declare @studId int; declare @studStatus bit; declare @dateDif int; declare @myCur cursor set @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
  • 17. 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
  • 18. Questions? “A good question deserve a good grade…”
  • 20. • 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 !!
  • 21. • 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 !!
  • 22. 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
  • 23. 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
  • 24. Self Check !! CREATE PROCEDURE simpleProc ( OUT param1 INT) BEGIN SELECT COUNT(*) INTO param1 FROM t ; END EXEC simpleProc(@a); Select @a ;
  • 25. Self Check !! CREATE PROCEDURE simpleProc ( @param1 INT) BEGIN SELECT COUNT(*) INTO param1 FROM t ; END exec simpleProc(@a); Select @a ;
  • 26. 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
  • 27. 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