Stored procedure

663 views

Published on

Stored procedure

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
663
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Stored procedure

  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. STORED POCEDURE ASNA.C.A kunjulaloos@gmail.com www.facebook.com/userna me twitter.com/username in.linkedin.com/in/profilena me 9048331776
  3. 3. • 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:
  4. 4. • Creating stored procure using server management studio (SSMS Step 1: Expand your database name node in SSMS. Then expand Programmability node as shown in the following screenshot:
  5. 5. Step 2: Right click on Stored Procedures node and click on New stored procedure...
  6. 6. • 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 ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- =============================================
  7. 7. 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> 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
  8. 8. 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_Proc AS BEGIN -- 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.
  9. 9. • 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.
  10. 10. 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 ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Exact Help -- Create date: 02/10/2012 -- Description: My first stored procedure -- ============================================= ALTER PROCEDURE [dbo].[My_First_Proc] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; SELECT 'Exact Help' END
  11. 11. Example CREATE PROCEDURE sp_GetInventory @location varchar(10) AS SELECT Product, Quantity FROM Inventory WHERE Warehouse = @location EXECUTE sp_GetInventory 'FL’
  12. 12. Advantages of stored procedure • Precompiled execution SQL Server compiles each stored procedure once and then reutilizes the execution plan. This results in tremendous performance boosts when stored procedures are called repeatedly. • Reduced client/server traffic If network bandwidth is a concern in your environment, you'll be happy to learn that stored procedures can reduce long SQLqueries to a single line that is transmitted over the wire.
  13. 13. • 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.
  14. 14. 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
  15. 15. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com

×