python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
check view exists sql server

What is store procedure? How do they work? When do you use?

A stored procedure is a collection of SQL statements that has been created and stored in the database.

It is a set of recompiled SQL statements that are used to perform a special task.

Stored procedures create once a time and calls it n number of times and also reduces the network traffic and increase the performance.

When do you use store procedure?
I used store procedures in 1 of 3 scenarios,
·         Security,
·         Speed and
·         Transactions

Types of SQL Procedures,
1.      System Stored Procedures
2.      User Defined Stored Procedures
3.      Extended Stored Procedures

Syntax:-
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
    -- Add the parameters for the stored procedure here
    <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO

Examples for creating Stored Procedure,
--FOR INSERT QUERY
------------------------------
CREATE PROCEDURE sp_NameofProc
(
 @Id INT,
 @Name VARCHAR(50),
 @Age  VARCHAR(50),
 @City VARCHAR(50)
)
AS BEGIN
INSERT INTO tableName(Id, [Name], Age, City) 
    VALUES(@Id,@Name,@Age,@City)
END


--FOR UPDATE QUERY
--------------------------------
CREATE PROCEDURE sp_NameofProc1
(
 @Id INT,
 @Name VARCHAR(50),
 @Age  VARCHAR(50),
 @City VARCHAR(50)
)
AS BEGIN
UPDATE tableName 
    SET Name='Anil' 
    WHERE Id=@Id
END


--FOR SELECT QUERY
----------------------------
CREATE PROCEDURE sp_NameofProc2
AS BEGIN
    SELECT * FROM TableName
END

--ALTER OR MODIFY A STORED PROCEDURE.
ALTER PROCEDURE sp_NameofProc
(
 @Id INT,
 @Name VARCHAR(50) = NULL,
 @Age  VARCHAR(50) = NULL,
 @City VARCHAR(50) = NULL
)
AS BEGIN
INSERT INTO tableName(Id, [Name], Age, City) 
    VALUES(@Id,@Name,@Age,@City)
END

--DROP OR DELETE A STORED PROCEDURE.
DROP PROCEDURE sp_NameofProc


The SQL Stored Procedure Summary:-
1.      Stored Procedure may or not return values.
2.      Stored Procedure can have both input and output parameters.
3.      Stored Procedures can call functions.
4.      We can use exception handling in the Stored Procedure using try catch blocks.
5.      We can use transactions within Stored Procedures but not in function.
6.      We can use both table variables as well as temporary table in it.
7.      We can’t use Stored Procedures in the SELECT, WHERE and HAVING statements.

  I hope it is helpful to you! Thank you!
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions