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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.