Stored procedure

17

description

Stored procedure

Transcript of Stored procedure

Page 1: Stored procedure
Page 2: Stored procedure

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Stored procedure

STORED POCEDURE

[email protected]/usernametwitter.com/usernamein.linkedin.com/in/profilename9048331776

Page 4: Stored procedure

• A stored procedure is a subroutine available to application that access a relational database system. A stored procedure is actually stored in the database data dictionary.

• Stored procedures are more than just tools for performing repetitive tasks. There are two main types of stored procedure – system stored procedures and user-defined stored procedures.

In sql server it is very easy to create stored procedure. We can create a stored procedure in sql server management studio (SSMS) in following way:

Page 5: Stored procedure

• Creating stored procure using server management studio (SSMS

Step 1: Expand your database name node inSSMS. Then expand Programmability node asshown in the following screenshot:

Page 6: Stored procedure

Step 2: Right click on Stored Procedures nodeand click on New stored procedure...

Page 7: Stored procedure

Step 3: It will generate following sql script in new query page:-- ================================================

-- Template generated from Template Explorer using:-- Create Procedure (New Menu).SQL---- Use the Specify Values for Template Parameters-- command (Ctrl-Shift-M) to fill in the parameter-- values below.---- This block of comments will not be included in-- the definition of the procedure.-- ================================================SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================

Page 8: Stored procedure

CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> -- Add the parameters for the stored procedure here <@Param1, sysname, @p1> <Datatype_For_Param1, , int> =<Default_Value_For_Para

m1, , 0>, <@Param2, sysname, @p2> <Datatype_For_Param2, , int> =<Default_Value_For_Para

m2, , 0>ASBEGIN -- 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>ENDGO

Page 9: Stored procedure

Step 4: Now edit above script like stored procedure name, parameters, procedure body etc according to your requirement . For example:

-- =============================================-- Author: Exact Help-- Create date: 02/10/2012-- Description: My first stored procedure-- =============================================CREATE PROCEDURE My_First_ProcASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;

SELECT 'Exact Help'END

GO

Step 5: To create My_First_Proc stored procedure press F5 button of your keyboard or click on Execute button in SSMS.

Page 10: Stored procedure

• Step 6: After creating it will be stored inside Programmability node. To re-open or edit it expand programmability node and right click on your stored procedure name and choose modify option.

Page 11: Stored procedure

You will get the script of your stored procedure. In this case it will be script of My_First_Proc. Which will something like this:

USE [Exact]GO/****** Object: StoredProcedure [dbo].[My_First_Proc] Script Date: 02/10/2012 10:02:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Exact Help-- Create date: 02/10/2012-- Description: My first stored procedure-- =============================================ALTER PROCEDURE [dbo].[My_First_Proc]ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;

SELECT 'Exact Help'END

Page 12: Stored procedure
Page 13: Stored procedure

Example

CREATE PROCEDURE sp_GetInventory@location varchar(10)ASSELECT Product, QuantityFROM InventoryWHERE Warehouse = @locationEXECUTE sp_GetInventory 'FL’

Page 14: Stored procedure

Advantages of stored procedure

• Precompiled executionSQL Server compiles each stored procedure once andthen reutilizes the execution plan. This results intremendous performance boosts when storedprocedures are called repeatedly.

• Reduced client/server trafficIf network bandwidth is a concern in your environment,you'll be happy to learn that stored procedures canreduce long SQLqueries to a single line that istransmitted over the wire.

Page 15: Stored procedure

• Efficient reuse of code and programming abstraction Stored procedures can be used by multiple users and client

programs. If you utilize them in a planned manner, you'll find the development cycle takes less time.

• Enhanced security controls You can grant users permission to execute a stored procedure

independently of underlying table permissions.

Page 16: Stored procedure

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 17: Stored procedure

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]