SlideShare una empresa de Scribd logo
1 de 14
Stored Procedure
By Deepak Sharma
Software Developer
What is Stored Procedure?
• A stored procedure is a group of sql
statements that has been created and stored
in the database.
• Stored procedure will accepts input
parameters so that a single procedure can be
used over network by several clients using
different input data.
Benefit of Stored Procedure
• Stored procedure will reduce network traffic
and increase the performance.
• If we modify stored procedure all clients will
get the updated stored procedure.
Types of Stored Procedure
1. System Stored Procedure
2. User Defined Stored Procedure
Types of User Defined Stored Procedure:-
1. Non Parameterized Stored Procedure
2. Parameterized Stored Procedure
--¤ USER DEFINED PROCEDURE-"NON
PARAMETERIZED STORED
PROCEDURE"
Firstly we have to create table:-
• create table contact(id int,name nvarchar(15),age int)
• select * from contact
• insert into contact select 1,'Deepak',23 union all select 2,'Shipra',23
union all select 3,'Richa',23 union all select 4,'Rashi',23
--¤ USER DEFINED PROCEDURE-"NON
PARAMETERIZED STORED
PROCEDURE"
Create Procedure:-
• create proc ProcDemo_select as select * from contact
Here proc declare as a procedure and you can also write procedure.
Fore execute this procedure :-
• exec ProcDemo_select
--¤ USER DEFINED PROCEDURE -
"PARAMETERIZED PROCEDURE"
• create proc ProcDemo_insert(@id int, @name nvarchar(15), @age int) as insert into
contact values(@id,@name,@age)
• exec ProcDemo_insert 4,'Aryan',23
• --or
• --ProcDemo_insert 5,'Hello',23
• exec ProcDemo_select
• -------------------------------------------------------------------------------------------------------------------
• create proc ProcDemo_update(@id int,@name nvarchar(15), @age int) as update
contact set name=@name, age=@age where id=@id
• exec ProcDemo_update 4,'Radha',23
• exec ProcDemo_select
• -------------------------------------------------------------------------------------------------------------------
• create proc ProcDemo_delete(@id int) as delete from contact where id=@id
• exec ProcDemo_delete 4
• exec ProcDemo_select
FOR VIEW ALL OF QUERY OF
STORED PROCEDURE
• sp_helptext ProcDemo_select
• sp_helptext ProcDemo_insert
• sp_helptext ProcDemo_update
• sp_helptext ProcDemo_delete
FOR DELETE STORED PROCEDURE
• drop proc ProcDemo_select
• drop proc ProcDemo_insert
• drop proc ProcDemo_update
• drop proc ProcDemo_delete
NAMING STRORED PROCEDURE
OBJECT
So some of these may be:
• uspInsertPerson - insert a new person record
• uspGetAccountBalance - get the balance of an account
• uspGetOrderHistory - return list of orders
STORED PROCEDURE USING TRY & CATCH BLOCK-:-
• CREATE PROCEDURE ProcDemo_TryCatch AS BEGIN TRY
SELECT * from contact
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS
ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS
ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS
ErrorMessage;
END CATCH
exec ProcDemo_TryCatch
FOR CALCULATING AVERAGE
• create proc ProcedureAverage as select AVG(id) as Average from contact
• exec ProcedureAverage
USING STORED PROCEDURE WITH ASP.NET
PAGE WITH TRANSACTION ON .ASPX PAGE-:-
create table test(id int,name varchar(20))
select * from test
insert test select 1,'Deepak' union all select 2,'Rashi'
create proc sp_Test(@id int,@name varchar(20)) as
begin try
insert into test values(@id,@name)
END TRY
BEGIN CATCH
SELECT
ERROR_LINE(),ERROR_NUMBER(),ERROR_PROCEDURE(),ERROR_SEVERITY(
),ERROR_STATE(),ERROR_MESSAGE();
END CATCH
drop proc sp_Test
allow a developer to work with transaction with two simple statement:
¤ Begin Transaction
¤ Commit Transaction
ADO.NET TRANSACTION:-
con.Open(); SqlTransaction trans; trans = con.BeginTransaction();
try
{
cmd = new SqlCommand("sp_Test", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = txtID.Text;
cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = txtName.Text.ToString();
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
Label1.Text = "Insert Fail";
}
con.Close(); con.Dispose();
Thanks
By Deepak Sharma
Software Developer

Más contenido relacionado

La actualidad más candente (20)

PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Stored procedure in sql server
Stored procedure in sql serverStored procedure in sql server
Stored procedure in sql server
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Oracle Database View
Oracle Database ViewOracle Database View
Oracle Database View
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Triggers
TriggersTriggers
Triggers
 
Trigger
TriggerTrigger
Trigger
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
MySQL Transactions
MySQL TransactionsMySQL Transactions
MySQL Transactions
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
PLSQL
PLSQLPLSQL
PLSQL
 

Destacado (15)

cv deepak sharma
cv deepak sharmacv deepak sharma
cv deepak sharma
 
Intro to cao &store program
Intro to cao &store programIntro to cao &store program
Intro to cao &store program
 
MVC by asp.net development company in india
MVC by asp.net development company in indiaMVC by asp.net development company in india
MVC by asp.net development company in india
 
Triggers ppt
Triggers pptTriggers ppt
Triggers ppt
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
DB2-SQL Part-2
DB2-SQL Part-2DB2-SQL Part-2
DB2-SQL Part-2
 
Ajax Ppt 1
Ajax Ppt 1Ajax Ppt 1
Ajax Ppt 1
 
2.computer org.
2.computer org.2.computer org.
2.computer org.
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
11 instruction sets addressing modes
11  instruction sets addressing modes 11  instruction sets addressing modes
11 instruction sets addressing modes
 
Addressing modes (detailed data path)
Addressing modes (detailed data path)Addressing modes (detailed data path)
Addressing modes (detailed data path)
 
Trigger
TriggerTrigger
Trigger
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 

Similar a Stored procedure

Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.CocoaHeads France
 
Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdfssuser28de9e
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performanceEngine Yard
 
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"Fwdays
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
Predix 따라하기 2
Predix 따라하기 2Predix 따라하기 2
Predix 따라하기 2HeeJung Park
 
SQL Server Deep Drive
SQL Server Deep Drive SQL Server Deep Drive
SQL Server Deep Drive DataArt
 
Labs_BT_20221017.pptx
Labs_BT_20221017.pptxLabs_BT_20221017.pptx
Labs_BT_20221017.pptxssuserb4d806
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sqlj9soto
 
2008 Collaborate IOUG Presentation
2008 Collaborate IOUG Presentation2008 Collaborate IOUG Presentation
2008 Collaborate IOUG PresentationBiju Thomas
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in ActionSveta Smirnova
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsZohar Elkayam
 
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능Hyperledger Korea User Group
 
Best Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLBest Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLEDB
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012Roland Bouman
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012Roland Bouman
 

Similar a Stored procedure (20)

Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.
 
Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdf
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Predix 따라하기 2
Predix 따라하기 2Predix 따라하기 2
Predix 따라하기 2
 
Hack through Injections
Hack through InjectionsHack through Injections
Hack through Injections
 
SQL Server Deep Drive
SQL Server Deep Drive SQL Server Deep Drive
SQL Server Deep Drive
 
Labs_BT_20221017.pptx
Labs_BT_20221017.pptxLabs_BT_20221017.pptx
Labs_BT_20221017.pptx
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sql
 
Rails Security
Rails SecurityRails Security
Rails Security
 
2008 Collaborate IOUG Presentation
2008 Collaborate IOUG Presentation2008 Collaborate IOUG Presentation
2008 Collaborate IOUG Presentation
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
 
Database training for developers
Database training for developersDatabase training for developers
Database training for developers
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
 
Best Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLBest Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQL
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
 

Último

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Stored procedure

  • 1. Stored Procedure By Deepak Sharma Software Developer
  • 2. What is Stored Procedure? • A stored procedure is a group of sql statements that has been created and stored in the database. • Stored procedure will accepts input parameters so that a single procedure can be used over network by several clients using different input data.
  • 3. Benefit of Stored Procedure • Stored procedure will reduce network traffic and increase the performance. • If we modify stored procedure all clients will get the updated stored procedure.
  • 4. Types of Stored Procedure 1. System Stored Procedure 2. User Defined Stored Procedure Types of User Defined Stored Procedure:- 1. Non Parameterized Stored Procedure 2. Parameterized Stored Procedure
  • 5. --¤ USER DEFINED PROCEDURE-"NON PARAMETERIZED STORED PROCEDURE" Firstly we have to create table:- • create table contact(id int,name nvarchar(15),age int) • select * from contact • insert into contact select 1,'Deepak',23 union all select 2,'Shipra',23 union all select 3,'Richa',23 union all select 4,'Rashi',23
  • 6. --¤ USER DEFINED PROCEDURE-"NON PARAMETERIZED STORED PROCEDURE" Create Procedure:- • create proc ProcDemo_select as select * from contact Here proc declare as a procedure and you can also write procedure. Fore execute this procedure :- • exec ProcDemo_select
  • 7. --¤ USER DEFINED PROCEDURE - "PARAMETERIZED PROCEDURE" • create proc ProcDemo_insert(@id int, @name nvarchar(15), @age int) as insert into contact values(@id,@name,@age) • exec ProcDemo_insert 4,'Aryan',23 • --or • --ProcDemo_insert 5,'Hello',23 • exec ProcDemo_select • ------------------------------------------------------------------------------------------------------------------- • create proc ProcDemo_update(@id int,@name nvarchar(15), @age int) as update contact set name=@name, age=@age where id=@id • exec ProcDemo_update 4,'Radha',23 • exec ProcDemo_select • ------------------------------------------------------------------------------------------------------------------- • create proc ProcDemo_delete(@id int) as delete from contact where id=@id • exec ProcDemo_delete 4 • exec ProcDemo_select
  • 8. FOR VIEW ALL OF QUERY OF STORED PROCEDURE • sp_helptext ProcDemo_select • sp_helptext ProcDemo_insert • sp_helptext ProcDemo_update • sp_helptext ProcDemo_delete
  • 9. FOR DELETE STORED PROCEDURE • drop proc ProcDemo_select • drop proc ProcDemo_insert • drop proc ProcDemo_update • drop proc ProcDemo_delete
  • 10. NAMING STRORED PROCEDURE OBJECT So some of these may be: • uspInsertPerson - insert a new person record • uspGetAccountBalance - get the balance of an account • uspGetOrderHistory - return list of orders
  • 11. STORED PROCEDURE USING TRY & CATCH BLOCK-:- • CREATE PROCEDURE ProcDemo_TryCatch AS BEGIN TRY SELECT * from contact END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; END CATCH exec ProcDemo_TryCatch FOR CALCULATING AVERAGE • create proc ProcedureAverage as select AVG(id) as Average from contact • exec ProcedureAverage
  • 12. USING STORED PROCEDURE WITH ASP.NET PAGE WITH TRANSACTION ON .ASPX PAGE-:- create table test(id int,name varchar(20)) select * from test insert test select 1,'Deepak' union all select 2,'Rashi' create proc sp_Test(@id int,@name varchar(20)) as begin try insert into test values(@id,@name) END TRY BEGIN CATCH SELECT ERROR_LINE(),ERROR_NUMBER(),ERROR_PROCEDURE(),ERROR_SEVERITY( ),ERROR_STATE(),ERROR_MESSAGE(); END CATCH drop proc sp_Test
  • 13. allow a developer to work with transaction with two simple statement: ¤ Begin Transaction ¤ Commit Transaction ADO.NET TRANSACTION:- con.Open(); SqlTransaction trans; trans = con.BeginTransaction(); try { cmd = new SqlCommand("sp_Test", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = txtID.Text; cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = txtName.Text.ToString(); cmd.Transaction = trans; cmd.ExecuteNonQuery(); trans.Commit(); } catch { trans.Rollback(); Label1.Text = "Insert Fail"; } con.Close(); con.Dispose();