SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Mysql:Operators
Mysql:OperatorsMysql:Operators
Mysql:Operators
 
Data types
Data typesData types
Data types
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
Cursors
CursorsCursors
Cursors
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
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 queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Sql select
Sql select Sql select
Sql select
 
MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Plsql
PlsqlPlsql
Plsql
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 

Andere mochten auch (17)

SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
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
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 

Kürzlich hochgeladen (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
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...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

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();