SlideShare ist ein Scribd-Unternehmen logo
1 von 25
STORED
PROCEDURES
CONTENTS
 Introduction to stored procedures
 Types of Stored Procedure
 User defined stored procedures
 Extended stored procedures
 System stored procedures
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.
 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
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
Catalog stored procedure:
sp_tables
exec sp_tables ‘%’,’dbo’,’students’
sp_stored_procedures
exec sp_stored_procedure ‘students’
sp_pkeys
exec sp_pkeys ‘student_info’
Database Engine Stored Procedures
 Sp_who:
sp_who[[@login_name=] ‘login’
execute sp_who
 Sp_help:
sp_help [ [@objectname= ] ‘name’]
execute sp_help ‘table_name’
 Sp_recompile:
sp_recompile [@objectnm =] ‘storedproc_name’
sp_recompile ‘customer’
Security Stored procedures:
sp_addlogin
sp_addlogin [ @loginame = ] 'login'
[ , [ @passwd = ] 'password' ]
[ , [ @defdb = ] 'database' ]
[ , [ @deflanguage = ] 'language' ]
execute sp_addlogin ‘abc’,’123’,’1106E1’
exec sp_adduser ‘abc’
sp_droplogin
execute sp_droplogin ‘login_name’
exec sp_droplogin ‘abc’
Full-Text Stored procedures:
 Sp_fulltext_catalog
exec sp_fulltext_catalog ‘ft_search’, ‘create’
• Sp_fulltext_table
exec sp_fulltext_table ‘customer’,’create’,’ft_search’,’pk_CustId’
• Sp_help_fulltext_tables
exec sp_help_fulltext_tables ‘ft_search’
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.
Create Stored Procedure
 CREATE PROCEDUREGetStudentIdentification
 AS
 BEGIN
 SELECT std_name, father_name,contact_number
 FROM students_information
 END
 GO
Execute Stored Proceedure
exec GetStudentIdentification
 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'
 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
 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'
Using Wildcard Characters:
Create procedure wild_cards
@name varchar(20)
as
Select * from employee where emp_name like @name
execute wild_cards 'D%'
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.
 CREATE PROCEDURE usp_adduser
 @login varchar(20),
@pswd varchar(20),
@f_name varchar(25),
@l_name varchar(35),
@address_1 varchar(30),
@address_2 varchar(30),
@city varchar(30),
@state char(2),
@zipcode char(10),
@email varchar(50)
 AS
 INSERT INTO USERLIST (login, pswd, f_name, l_name, address_1,
address_2, city, state, zipcode, email)
 VALUES (@login, @pswd, @f_name, @l_name, @address_1,
@address_2, @city, @state, @zipcode, @email)
 exec usp_adduser ‘dnelson’, ‘dean2003′,
‘Dean’, ‘Nelson’, ’200 Berkeley Street’, ‘ ‘,
‘Boston’, ‘MA’, ’02116′, ‘dnelson@test.com’
 CREATE PROCEDURE usp_updateuser
 @usr_id int,@login varchar(20),@pswd varchar(20),@f_name
varchar(25),
@l_name varchar(35),@address_1 varchar(30),@address_2
varchar(30),@city varchar(30),@state char(2),@zipcode
char(10),@email varchar(50)
 AS
 UPDATE USERLIST
 SET
 login=@login, pswd=@pswd,
f_name=@f_name, l_name=@l_name,
address_1=@address_1,
address_2=@address_2,
city=@city, state=@state,
zipcode=@zipcode, email=@email
 WHERE usr_id=@usr_id
 CREATE PROCEDURE usp_finduser
 @usr_id int
 AS
 SELECT * FROM USERLIST
WHERE usr_id=@usr_id
 Execute Statement:
 exec usp_finduser ’1′
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
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.
ERROR Function
 Error_line()
 Error_number()
 Error_message()
 Error_procedure()
 Error_severity()
 Error_state()
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
Sp_addmessage stored procedure
Execute sp_addmessage @msgnum=50001,
@severity=10,
@msgtext=‘This is a customized error’,
@lang=‘us_english’
Raiserror(50001,10,1)

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Intro to tsql unit 6
Intro to tsql   unit 6Intro to tsql   unit 6
Intro to tsql unit 6
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
SQL
SQLSQL
SQL
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure Presentation
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Triggers and Stored Procedures
Triggers and Stored ProceduresTriggers and Stored Procedures
Triggers and Stored Procedures
 
oracle plsql training | oracle online training | oracle plsql demo | oracle p...
oracle plsql training | oracle online training | oracle plsql demo | oracle p...oracle plsql training | oracle online training | oracle plsql demo | oracle p...
oracle plsql training | oracle online training | oracle plsql demo | oracle p...
 

Ähnlich wie Sql server ___________session_18(stored procedures)

How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfAppweb Coders
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile appsIvano Malavolta
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiMuhammed Thanveer M
 
SQLite in Adobe AIR
SQLite in Adobe AIRSQLite in Adobe AIR
SQLite in Adobe AIRPeter Elst
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Michelangelo van Dam
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web appsIvano Malavolta
 
Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Michelangelo van Dam
 
VPN Access Runbook
VPN Access RunbookVPN Access Runbook
VPN Access RunbookTaha Shakeel
 
SQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdfSQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdfMona686896
 
Тестирование и Django
Тестирование и DjangoТестирование и Django
Тестирование и DjangoMoscowDjango
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
Core Php Component Presentation
Core Php Component PresentationCore Php Component Presentation
Core Php Component PresentationJohn Coonen
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8PrinceGuru MS
 

Ähnlich wie Sql server ___________session_18(stored procedures) (20)

How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdf
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile apps
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
 
SQLite in Adobe AIR
SQLite in Adobe AIRSQLite in Adobe AIR
SQLite in Adobe AIR
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web apps
 
Msql
Msql Msql
Msql
 
Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012
 
Php summary
Php summaryPhp summary
Php summary
 
SQL Tracing
SQL TracingSQL Tracing
SQL Tracing
 
VPN Access Runbook
VPN Access RunbookVPN Access Runbook
VPN Access Runbook
 
SQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdfSQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdf
 
Тестирование и Django
Тестирование и DjangoТестирование и Django
Тестирование и Django
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Sql injection
Sql injectionSql injection
Sql injection
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Core Php Component Presentation
Core Php Component PresentationCore Php Component Presentation
Core Php Component Presentation
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 

Mehr von Ehtisham Ali

Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)Ehtisham Ali
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzationEhtisham Ali
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modelingEhtisham Ali
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)Ehtisham Ali
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)Ehtisham Ali
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)Ehtisham Ali
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)Ehtisham Ali
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)Ehtisham Ali
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)Ehtisham Ali
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-introEhtisham Ali
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)Ehtisham Ali
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)Ehtisham Ali
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)Ehtisham Ali
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql serverEhtisham Ali
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control languageEhtisham Ali
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)Ehtisham Ali
 

Mehr von Ehtisham Ali (17)

Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modeling
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-intro
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql server
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control language
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)
 

Kürzlich hochgeladen

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 

Kürzlich hochgeladen (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Sql server ___________session_18(stored procedures)

  • 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
  • 6. Catalog stored procedure: sp_tables exec sp_tables ‘%’,’dbo’,’students’ sp_stored_procedures exec sp_stored_procedure ‘students’ sp_pkeys exec sp_pkeys ‘student_info’
  • 7. Database Engine Stored Procedures  Sp_who: sp_who[[@login_name=] ‘login’ execute sp_who  Sp_help: sp_help [ [@objectname= ] ‘name’] execute sp_help ‘table_name’  Sp_recompile: sp_recompile [@objectnm =] ‘storedproc_name’ sp_recompile ‘customer’
  • 8. Security Stored procedures: sp_addlogin sp_addlogin [ @loginame = ] 'login' [ , [ @passwd = ] 'password' ] [ , [ @defdb = ] 'database' ] [ , [ @deflanguage = ] 'language' ] execute sp_addlogin ‘abc’,’123’,’1106E1’ exec sp_adduser ‘abc’ sp_droplogin execute sp_droplogin ‘login_name’ exec sp_droplogin ‘abc’
  • 9. Full-Text Stored procedures:  Sp_fulltext_catalog exec sp_fulltext_catalog ‘ft_search’, ‘create’ • Sp_fulltext_table exec sp_fulltext_table ‘customer’,’create’,’ft_search’,’pk_CustId’ • Sp_help_fulltext_tables exec sp_help_fulltext_tables ‘ft_search’
  • 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.
  • 17.  CREATE PROCEDURE usp_adduser  @login varchar(20), @pswd varchar(20), @f_name varchar(25), @l_name varchar(35), @address_1 varchar(30), @address_2 varchar(30), @city varchar(30), @state char(2), @zipcode char(10), @email varchar(50)  AS  INSERT INTO USERLIST (login, pswd, f_name, l_name, address_1, address_2, city, state, zipcode, email)  VALUES (@login, @pswd, @f_name, @l_name, @address_1, @address_2, @city, @state, @zipcode, @email)
  • 18.  exec usp_adduser ‘dnelson’, ‘dean2003′, ‘Dean’, ‘Nelson’, ’200 Berkeley Street’, ‘ ‘, ‘Boston’, ‘MA’, ’02116′, ‘dnelson@test.com’
  • 19.  CREATE PROCEDURE usp_updateuser  @usr_id int,@login varchar(20),@pswd varchar(20),@f_name varchar(25), @l_name varchar(35),@address_1 varchar(30),@address_2 varchar(30),@city varchar(30),@state char(2),@zipcode char(10),@email varchar(50)  AS  UPDATE USERLIST  SET  login=@login, pswd=@pswd, f_name=@f_name, l_name=@l_name, address_1=@address_1, address_2=@address_2, city=@city, state=@state, zipcode=@zipcode, email=@email  WHERE usr_id=@usr_id
  • 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.
  • 23. ERROR Function  Error_line()  Error_number()  Error_message()  Error_procedure()  Error_severity()  Error_state()
  • 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)