0% found this document useful (0 votes)
94 views5 pages

Store Procedure

Sql

Uploaded by

Guru Sumanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views5 pages

Store Procedure

Sql

Uploaded by

Guru Sumanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What is a Stored Procedure (SP) ?

Stored Procedure
• SQL Server stored procedure is a batch of statements grouped as a logical unit and stored in the database.

• The stored procedure accepts the parameters and executes the T-SQL statements in the procedure, returns the
result set if any.

• A stored procedure is a type of code in SQL that can be stored for later use and can be used many times.

• So, whenever you need to execute the query, instead of calling it you can just call the stored procedure.

• You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter
values that is passed.
Advantages of using stored procedure
• Better Performance: The procedure calls are quick and efficient as stored procedures are compiled once and
stored in executable form. Hence the response is quick. The executable code is automatically cached, hence
lowers the memory requirements.

• Reduced network traffic: When we use stored procedures instead of writing T-SQL queries at the application
level, only the procedure name is passed over the network instead of the whole T-SQL code.

• Reusable: Stored procedures can be executed by multiple users or multiple client applications without the need of
writing the code again.

• Security: Stored procedures reduce the threat by eliminating direct access to the tables. we can also encrypt the
stored procedures while creating them so that source code inside the stored procedure is not visible.

• It can be easily modified: We can easily modify the code inside the stored procedure without the need to restart or
deploying the application. For example, If the T-SQL queries are written in the application and if we need to change
the logic, we must change the code in the application and re-deploy it. SQL Server Stored procedures eliminate
such challenges by storing the code in the database. so, when we want to change the logic inside the procedure we
can just do it by simple ALTER PROCEDURE statement.
Syntax of stored procedure
• Create Statement for a stored procedure

• CREATE PROCEDURE procedure_name


AS
sql_statement
GO;

• Execute a stored procedure


• EXEC procedure_name;

• CREATE PROCEDURE SelectAllPersonAddress


AS
SELECT * FROM Person.Address
GO;

• EXEC SelectAllPersonAddress ;
Syntax of stored procedure with parameter(s)
• Create Statement for a stored procedure with parameter

• CREATE PROCEDURE procedure_name @param1 type, @param2 type


AS
sql_statement
GO;

• Execute a stored procedure


• EXEC procedure_name param1, param2;

• CREATE PROCEDURE SelectAllPersonAddress @City navarchar(30)


AS
SELECT * FROM Person.Address where city = @city
GO;

• EXEC SelectAllPersonAddress @city = ‘New York’ ;

• EXEC SelectAllPersonAddress ‘New York’ ;

You might also like