SlideShare una empresa de Scribd logo
1 de 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)

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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...
 

Similar a STORED PROCEDURES GUIDE

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
 

Similar a STORED PROCEDURES GUIDE (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
 

Más de 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
 

Más de 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)
 

Último

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Último (20)

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 

STORED PROCEDURES GUIDE

  • 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)