SlideShare a Scribd company logo
Stored Procedure: Stored Procedure in SQL Server can be defined as the set of logical group of SQL
statements which are grouped to perform a specific task. There are many benefits of using a stored
procedure. The main benefit of using a stored procedure is that it increases the performance of the
database.The other benefits of using the Stored Procedure are given below.
Benefits of Using the Stored Procedure
1. One of the main benefits of using the Stored procedure is that it reduces the amount of information
sent to the database server. It can become a more important benefit when the bandwidth of the
network is less. Since if we send the SQL query (statement) which is executing in a loop to the server
through network and the network gets disconnected, then the execution of the SQL statement doesn't
return the expected results, if the SQL query is not used between Transaction statement and rollback
statement is not used.
2. Compilation step is required only once when the stored procedure is created. Then after it does not
require recompilation before executing unless it is modified and reutilizes the same execution plan
whereas the SQL statements need to be compiled every time whenever it is sent for execution even if
we send the same SQL statement every time.
3. It helps in re usability of the SQL code because it can be used by multiple users and by multiple
clients since we need to just call the stored procedure instead of writing the same SQL statement
every time. It helps in reducing the development time.
4. Stored procedure is helpful in enhancing the security since we can grant permission to the user for
executing the Stored procedure instead of giving permission on the tables used in the Stored
procedure.
5. Sometimes, it is useful to use the database for storing the business logic in the form of stored
procedure since it makes it secure and if any change is needed in the business logic, then we may
only need to make changes in the stored procedure and not in the files contained on the web server.
How to Write a Stored Procedure in SQL Server
Suppose there is a table called tbl_Students whose structure is given below:
CREATE TABLE tbl_Students
(
[Studentid] [int] IDENTITY(1,1) NOT NULL,
[Firstname] [nvarchar](200) NOT NULL,
[Lastname] [nvarchar](200) NULL,
[Email] [nvarchar](100) NULL
)
Support we insert the following data into the above table:
Insert into tbl_Students (Firstname, lastname, Email)
Values('Vivek', 'Johari', 'vivek@abc.com')
Insert into tbl_Students (Firstname, lastname, Email)
Values('Pankaj', 'Kumar', 'pankaj@abc.com')
Insert into tbl_Students (Firstname, lastname, Email)
Values('Amit', 'Singh', 'amit@abc.com')
Insert into tbl_Students (Firstname, lastname, Email)
Values('Manish', 'Kumar', 'manish@abc.comm')
Article by: Durgesh Kr. Singh
Software Developer(Star Group of Comp.)
Insert into tbl_Students (Firstname, lastname, Email)
Values('Abhishek', 'Singh', 'abhishek@abc.com')
Now, while writing a Stored Procedure, the first step will be to write the Create Procedure statement as
the first statement:
Create Procedure Procedure-name
(
Input parameters ,
Output Parameters (If required)
)
As
Begin
Sql statement used in the stored procedure
End
Now, suppose we need to create a Stored Procedure which will return a student name whose studentid is
given as the input parameter to the stored procedure. Then, the Stored Procedure will be:
/* Getstudentname is the name of the stored procedure*/
Create PROCEDURE Getstudentname(
@studentid INT --Input parameter , Studentid of the student
)
AS
BEGIN
SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid
END
We can also collect the student name in the output parameter of the Stored Procedure. For example:
/*
GetstudentnameInOutputVariable is the name of the stored procedure which
uses output variable @Studentname to collect the student name returns by the
stored procedure
*/
Create PROCEDURE GetstudentnameInOutputVariable
(
@studentid INT, --Input parameter , Studentid of the student
@studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT
keyword
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE
studentid=@studentid
END
Note:-/* */ is used to write comments in one or multiple lines
-- is used to write a comment in a single line
How to Alter a Stored Procedure in a SQL Server
In SQL Server, a stored procedure can be modified with the help of the Alter keyword. Now if we want to
get student email address through the same procedure GetstudentnameInOutputVariable. So we need
to modify it by adding one more output parameter "@StudentEmail " which is shown below:
/*
Stored Procedure GetstudentnameInOutputVariable is modified to collect the
email address of the student with the help of the Alert Keyword
*/
Alter PROCEDURE GetstudentnameInOutputVariable
(
@studentid INT, --Input parameter , Studentid of the student
@studentname VARCHAR (200) OUT, -- Output parameter to collect the student name
@StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname,
@StudentEmail=email FROM tbl_Students WHERE studentid=@studentid
END
Note: It is not necessary that a stored procedure will have to return. There can be a case when a stored
procedure doesn't returns anything. For example, a stored procedure can be used to Insert, delete or
update a SQL statement. For example, the below stored procedure is used to insert value into the table
tbl_students.
/*
This Stored procedure is used to Insert value into the table tbl_students.
*/
Create Procedure InsertStudentrecord
(
@StudentFirstName Varchar(200),
@StudentLastName Varchar(200),
@StudentEmail Varchar(50)
)
As
Begin
Insert into tbl_Students (Firstname, lastname, Email)
Values(@StudentFirstName, @StudentLastName,@StudentEmail)
End
Execution of the Stored Procedure in SQL Server
Execution of the Stored Procedure which doesn't have an Output Parameter
A stored procedure is used in the SQL Server with the help of the "Execute" or "Exec" Keyword. For
example, if we want to execute the stored procedure "Getstudentname", then we will use the following
statement.
Execute Getstudentname 1
Exec Getstudentname 1
Execution of the Stored Procedure using the Output Parameter
If we want to execute the Stored procedure "GetstudentnameInOutputVariable" , then we first need to
declare the variable to collect the output values. For example:
Declare @Studentname as nvarchar(200) -- Declaring the variable to collect the
Studentname
Declare @Studentemail as nvarchar(50) -- Declaring the variable to collect the
Studentemail
Execute GetstudentnameInOutputVariable 1 , @Studentname output, @Studentemail output
select @Studentname,@Studentemail -- "Select" Statement is used to show the output
from Procedure ***
Ad

More Related Content

What's hot (20)

MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
DataminingTools Inc
 
ADO.Net Improvements in .Net 2.0
ADO.Net Improvements in .Net 2.0ADO.Net Improvements in .Net 2.0
ADO.Net Improvements in .Net 2.0
David Truxall
 
Intro to tsql unit 6
Intro to tsql   unit 6Intro to tsql   unit 6
Intro to tsql unit 6
Syed Asrarali
 
Change tracking
Change trackingChange tracking
Change tracking
Sonny56
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
Syed Asrarali
 
MySQL Views
MySQL ViewsMySQL Views
MySQL Views
Reggie Niccolo Santos
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
Manish Mudhliyar
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
Pongsakorn U-chupala
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
spring-tutorial
spring-tutorialspring-tutorial
spring-tutorial
Arjun Shanka
 
Database administration commands
Database administration commands Database administration commands
Database administration commands
Varsha Ajith
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Stored procedure
Stored procedureStored procedure
Stored procedure
baabtra.com - No. 1 supplier of quality freshers
 
Database Connection With Mysql
Database Connection With MysqlDatabase Connection With Mysql
Database Connection With Mysql
Harit Kothari
 
Refreshing mule cache using oracle database change notification
Refreshing mule cache using oracle database change notificationRefreshing mule cache using oracle database change notification
Refreshing mule cache using oracle database change notification
Priyobroto Ghosh (Mule ESB Certified)
 
Datamigration
DatamigrationDatamigration
Datamigration
Battlecruiser Vodanh
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
DataminingTools Inc
 
MySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs AcademyMySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs Academy
thewebsacademy
 

Similar to Stored procedure Notes By Durgesh Singh (20)

STORED-PROCEDURE.pptxjsjjdjdjcjcjdkksksksk
STORED-PROCEDURE.pptxjsjjdjdjcjcjdkkskskskSTORED-PROCEDURE.pptxjsjjdjdjcjcjdkksksksk
STORED-PROCEDURE.pptxjsjjdjdjcjcjdkksksksk
loreinesel
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrjPLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
ftz 420
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
Msql
Msql Msql
Msql
ksujitha
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
Muhammed Thanveer M
 
Passing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flowPassing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flow
Priyobroto Ghosh (Mule ESB Certified)
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
Syed Asrarali
 
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure Presentation
Amin Uddin
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
Dhananjay Goel
 
Mysql
MysqlMysql
Mysql
ksujitha
 
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptxDBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
universalcomputer1
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
Thuan Nguyen
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
Sankhya_Analytics
 
Based on the materials for this week, create your own unique Datab.docx
Based on the materials for this week, create your own unique Datab.docxBased on the materials for this week, create your own unique Datab.docx
Based on the materials for this week, create your own unique Datab.docx
JASS44
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
jain.pralabh
 
Using triggers in my sql database
Using triggers in my sql databaseUsing triggers in my sql database
Using triggers in my sql database
Mbarara University of Science and technology
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX Concepts
Gaurish Goel
 
Introducing ms sql_server_updated
Introducing ms sql_server_updatedIntroducing ms sql_server_updated
Introducing ms sql_server_updated
leetinhf
 
STORED-PROCEDURE.pptxjsjjdjdjcjcjdkksksksk
STORED-PROCEDURE.pptxjsjjdjdjcjcjdkkskskskSTORED-PROCEDURE.pptxjsjjdjdjcjcjdkksksksk
STORED-PROCEDURE.pptxjsjjdjdjcjcjdkksksksk
loreinesel
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrjPLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
ftz 420
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
Muhammed Thanveer M
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
Syed Asrarali
 
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure Presentation
Amin Uddin
 
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptxDBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
universalcomputer1
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
Thuan Nguyen
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
Sankhya_Analytics
 
Based on the materials for this week, create your own unique Datab.docx
Based on the materials for this week, create your own unique Datab.docxBased on the materials for this week, create your own unique Datab.docx
Based on the materials for this week, create your own unique Datab.docx
JASS44
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
jain.pralabh
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX Concepts
Gaurish Goel
 
Introducing ms sql_server_updated
Introducing ms sql_server_updatedIntroducing ms sql_server_updated
Introducing ms sql_server_updated
leetinhf
 
Ad

More from imdurgesh (20)

Azure quick-start-for-net-developers
Azure quick-start-for-net-developersAzure quick-start-for-net-developers
Azure quick-start-for-net-developers
imdurgesh
 
Sales Negotiating-for-entrepreneurs
Sales Negotiating-for-entrepreneursSales Negotiating-for-entrepreneurs
Sales Negotiating-for-entrepreneurs
imdurgesh
 
Automated testing-whitepaper
Automated testing-whitepaperAutomated testing-whitepaper
Automated testing-whitepaper
imdurgesh
 
Visual studio-2019-succinctly
Visual studio-2019-succinctlyVisual studio-2019-succinctly
Visual studio-2019-succinctly
imdurgesh
 
C# and .net framework
C# and .net frameworkC# and .net framework
C# and .net framework
imdurgesh
 
30 days-of-react-ebook-fullstackio
30 days-of-react-ebook-fullstackio30 days-of-react-ebook-fullstackio
30 days-of-react-ebook-fullstackio
imdurgesh
 
Linq pad succinctly
Linq pad succinctlyLinq pad succinctly
Linq pad succinctly
imdurgesh
 
ViA Bootstrap 4
ViA Bootstrap 4ViA Bootstrap 4
ViA Bootstrap 4
imdurgesh
 
The road-to-learn-react
The road-to-learn-reactThe road-to-learn-react
The road-to-learn-react
imdurgesh
 
Public speaking for_geeks_succinctly
Public speaking for_geeks_succinctlyPublic speaking for_geeks_succinctly
Public speaking for_geeks_succinctly
imdurgesh
 
Google maps api_succinctly
Google maps api_succinctlyGoogle maps api_succinctly
Google maps api_succinctly
imdurgesh
 
W3 css succinctly
W3 css succinctlyW3 css succinctly
W3 css succinctly
imdurgesh
 
Aspnet core-2-succinctly
Aspnet core-2-succinctlyAspnet core-2-succinctly
Aspnet core-2-succinctly
imdurgesh
 
Cryptography in net_succinctly
Cryptography in net_succinctlyCryptography in net_succinctly
Cryptography in net_succinctly
imdurgesh
 
Azure functions-succinctly
Azure functions-succinctlyAzure functions-succinctly
Azure functions-succinctly
imdurgesh
 
Nodejs succinctly
Nodejs succinctlyNodejs succinctly
Nodejs succinctly
imdurgesh
 
Angular succinctly
Angular succinctlyAngular succinctly
Angular succinctly
imdurgesh
 
Reactjs succinctly
Reactjs succinctlyReactjs succinctly
Reactjs succinctly
imdurgesh
 
C sharp code_contracts_succinctly
C sharp code_contracts_succinctlyC sharp code_contracts_succinctly
C sharp code_contracts_succinctly
imdurgesh
 
Asp.net tutorial
Asp.net tutorialAsp.net tutorial
Asp.net tutorial
imdurgesh
 
Azure quick-start-for-net-developers
Azure quick-start-for-net-developersAzure quick-start-for-net-developers
Azure quick-start-for-net-developers
imdurgesh
 
Sales Negotiating-for-entrepreneurs
Sales Negotiating-for-entrepreneursSales Negotiating-for-entrepreneurs
Sales Negotiating-for-entrepreneurs
imdurgesh
 
Automated testing-whitepaper
Automated testing-whitepaperAutomated testing-whitepaper
Automated testing-whitepaper
imdurgesh
 
Visual studio-2019-succinctly
Visual studio-2019-succinctlyVisual studio-2019-succinctly
Visual studio-2019-succinctly
imdurgesh
 
C# and .net framework
C# and .net frameworkC# and .net framework
C# and .net framework
imdurgesh
 
30 days-of-react-ebook-fullstackio
30 days-of-react-ebook-fullstackio30 days-of-react-ebook-fullstackio
30 days-of-react-ebook-fullstackio
imdurgesh
 
Linq pad succinctly
Linq pad succinctlyLinq pad succinctly
Linq pad succinctly
imdurgesh
 
ViA Bootstrap 4
ViA Bootstrap 4ViA Bootstrap 4
ViA Bootstrap 4
imdurgesh
 
The road-to-learn-react
The road-to-learn-reactThe road-to-learn-react
The road-to-learn-react
imdurgesh
 
Public speaking for_geeks_succinctly
Public speaking for_geeks_succinctlyPublic speaking for_geeks_succinctly
Public speaking for_geeks_succinctly
imdurgesh
 
Google maps api_succinctly
Google maps api_succinctlyGoogle maps api_succinctly
Google maps api_succinctly
imdurgesh
 
W3 css succinctly
W3 css succinctlyW3 css succinctly
W3 css succinctly
imdurgesh
 
Aspnet core-2-succinctly
Aspnet core-2-succinctlyAspnet core-2-succinctly
Aspnet core-2-succinctly
imdurgesh
 
Cryptography in net_succinctly
Cryptography in net_succinctlyCryptography in net_succinctly
Cryptography in net_succinctly
imdurgesh
 
Azure functions-succinctly
Azure functions-succinctlyAzure functions-succinctly
Azure functions-succinctly
imdurgesh
 
Nodejs succinctly
Nodejs succinctlyNodejs succinctly
Nodejs succinctly
imdurgesh
 
Angular succinctly
Angular succinctlyAngular succinctly
Angular succinctly
imdurgesh
 
Reactjs succinctly
Reactjs succinctlyReactjs succinctly
Reactjs succinctly
imdurgesh
 
C sharp code_contracts_succinctly
C sharp code_contracts_succinctlyC sharp code_contracts_succinctly
C sharp code_contracts_succinctly
imdurgesh
 
Asp.net tutorial
Asp.net tutorialAsp.net tutorial
Asp.net tutorial
imdurgesh
 
Ad

Recently uploaded (20)

Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New VersionPixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
saimabibi60507
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New VersionPixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
saimabibi60507
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 

Stored procedure Notes By Durgesh Singh

  • 1. Stored Procedure: Stored Procedure in SQL Server can be defined as the set of logical group of SQL statements which are grouped to perform a specific task. There are many benefits of using a stored procedure. The main benefit of using a stored procedure is that it increases the performance of the database.The other benefits of using the Stored Procedure are given below. Benefits of Using the Stored Procedure 1. One of the main benefits of using the Stored procedure is that it reduces the amount of information sent to the database server. It can become a more important benefit when the bandwidth of the network is less. Since if we send the SQL query (statement) which is executing in a loop to the server through network and the network gets disconnected, then the execution of the SQL statement doesn't return the expected results, if the SQL query is not used between Transaction statement and rollback statement is not used. 2. Compilation step is required only once when the stored procedure is created. Then after it does not require recompilation before executing unless it is modified and reutilizes the same execution plan whereas the SQL statements need to be compiled every time whenever it is sent for execution even if we send the same SQL statement every time. 3. It helps in re usability of the SQL code because it can be used by multiple users and by multiple clients since we need to just call the stored procedure instead of writing the same SQL statement every time. It helps in reducing the development time. 4. Stored procedure is helpful in enhancing the security since we can grant permission to the user for executing the Stored procedure instead of giving permission on the tables used in the Stored procedure. 5. Sometimes, it is useful to use the database for storing the business logic in the form of stored procedure since it makes it secure and if any change is needed in the business logic, then we may only need to make changes in the stored procedure and not in the files contained on the web server. How to Write a Stored Procedure in SQL Server Suppose there is a table called tbl_Students whose structure is given below: CREATE TABLE tbl_Students ( [Studentid] [int] IDENTITY(1,1) NOT NULL, [Firstname] [nvarchar](200) NOT NULL, [Lastname] [nvarchar](200) NULL, [Email] [nvarchar](100) NULL ) Support we insert the following data into the above table: Insert into tbl_Students (Firstname, lastname, Email) Values('Vivek', 'Johari', '[email protected]') Insert into tbl_Students (Firstname, lastname, Email) Values('Pankaj', 'Kumar', '[email protected]') Insert into tbl_Students (Firstname, lastname, Email) Values('Amit', 'Singh', '[email protected]') Insert into tbl_Students (Firstname, lastname, Email) Values('Manish', 'Kumar', '[email protected]') Article by: Durgesh Kr. Singh Software Developer(Star Group of Comp.)
  • 2. Insert into tbl_Students (Firstname, lastname, Email) Values('Abhishek', 'Singh', '[email protected]') Now, while writing a Stored Procedure, the first step will be to write the Create Procedure statement as the first statement: Create Procedure Procedure-name ( Input parameters , Output Parameters (If required) ) As Begin Sql statement used in the stored procedure End Now, suppose we need to create a Stored Procedure which will return a student name whose studentid is given as the input parameter to the stored procedure. Then, the Stored Procedure will be: /* Getstudentname is the name of the stored procedure*/ Create PROCEDURE Getstudentname( @studentid INT --Input parameter , Studentid of the student ) AS BEGIN SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END We can also collect the student name in the output parameter of the Stored Procedure. For example: /* GetstudentnameInOutputVariable is the name of the stored procedure which uses output variable @Studentname to collect the student name returns by the stored procedure */ Create PROCEDURE GetstudentnameInOutputVariable ( @studentid INT, --Input parameter , Studentid of the student @studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT keyword ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END Note:-/* */ is used to write comments in one or multiple lines -- is used to write a comment in a single line How to Alter a Stored Procedure in a SQL Server In SQL Server, a stored procedure can be modified with the help of the Alter keyword. Now if we want to get student email address through the same procedure GetstudentnameInOutputVariable. So we need to modify it by adding one more output parameter "@StudentEmail " which is shown below:
  • 3. /* Stored Procedure GetstudentnameInOutputVariable is modified to collect the email address of the student with the help of the Alert Keyword */ Alter PROCEDURE GetstudentnameInOutputVariable ( @studentid INT, --Input parameter , Studentid of the student @studentname VARCHAR (200) OUT, -- Output parameter to collect the student name @StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname, @StudentEmail=email FROM tbl_Students WHERE studentid=@studentid END Note: It is not necessary that a stored procedure will have to return. There can be a case when a stored procedure doesn't returns anything. For example, a stored procedure can be used to Insert, delete or update a SQL statement. For example, the below stored procedure is used to insert value into the table tbl_students. /* This Stored procedure is used to Insert value into the table tbl_students. */ Create Procedure InsertStudentrecord ( @StudentFirstName Varchar(200), @StudentLastName Varchar(200), @StudentEmail Varchar(50) ) As Begin Insert into tbl_Students (Firstname, lastname, Email) Values(@StudentFirstName, @StudentLastName,@StudentEmail) End Execution of the Stored Procedure in SQL Server Execution of the Stored Procedure which doesn't have an Output Parameter A stored procedure is used in the SQL Server with the help of the "Execute" or "Exec" Keyword. For example, if we want to execute the stored procedure "Getstudentname", then we will use the following statement. Execute Getstudentname 1 Exec Getstudentname 1 Execution of the Stored Procedure using the Output Parameter If we want to execute the Stored procedure "GetstudentnameInOutputVariable" , then we first need to declare the variable to collect the output values. For example: Declare @Studentname as nvarchar(200) -- Declaring the variable to collect the Studentname Declare @Studentemail as nvarchar(50) -- Declaring the variable to collect the Studentemail Execute GetstudentnameInOutputVariable 1 , @Studentname output, @Studentemail output select @Studentname,@Studentemail -- "Select" Statement is used to show the output from Procedure ***