Sql storeprocedure

1,742 views
1,607 views

Published on

About Stored Procedures using in sql&pl/sql

Published in: Education, Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
1,742
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
99
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Sql storeprocedure

  1. 1. PRESENTATION <br />STORE PROCEDURE<br />Presented By – <br />Prabhu P<br />
  2. 2. Overview<br /><ul><li>About Store Procedure
  3. 3. Difference between SP and Function
  4. 4. How to create a store procedure
  5. 5. How to call.
  6. 6. Initial Processing - Review</li></ul>Resolution<br />Compilation/Optimization<br />Execution/Recompilation<br /><ul><li>Recompilation Issues </li></ul>When do you want to Recompile?<br />Options for Recompilation? <br />What to Recompile?<br />
  7. 7. What is store procedure?<br />A stored procedure is a set of SQL commands that has been compiled and stored on the database server.<br /> Once the stored procedure has been "stored", client applications can execute the stored procedure over and over again without sending it to the database server again and without compiling it again.<br /> Stored procedures improve performance by reducing network traffic and CPU load.<br />
  8. 8. Comparison with dynamic SQL<br /><ul><li>Remove overhead
  9. 9. Avoidance of network traffic
  10. 10. Encapsulation of business logic
  11. 11. Delegation of access-rights
  12. 12. Some protection from SQL injection attacks</li></li></ul><li>Comparison with functions<br /><ul><li>1.Procedure can return zero or n values whereas function can return one value which is mandatory.2.Procedures can have input,output parameters for it whereas functions can have only input parameters.3.Procedure allow select as well as DML statement in it whereas function allow only select statement in it.4.Functions can be called from procedure whereas procedures cannot be called from function.5.Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.6.We can go for transaction management in procedure whereas we can't go in function.7.Functions can run an executable file from SQL SELECT or an action query.operating system use Execute or Exec to run </li></li></ul><li>How To Create And Execute SP<br />There are various options that can be used to create stored procedures.  In these next few topics we will discuss creating a stored procedure and How to execute.<br />
  13. 13. Syntax Of Store ProcedureWith Parameter<br />-- =============================================<br />-- Author: <Author,,Name><br />-- Create date: <Create Date,,><br />-- Description:<Description,,><br />-- =============================================<br />Create Proc SP_Name<br />(<br /> Param1 DataType,<br /> param2 DataType,<br /> Param3 DataType,<br /> .<br /> .<br /> .<br />ParamnDataType<br />)<br />As<br /> Begin<br /> Body Of Store Procedure<br /> End<br />
  14. 14. Implementation:<br />-- =============================================<br /> -- Author: <Author: XYZ><br /> -- Create date: <Create Date:23/06/2010><br /> -- Description: < Used to retrieve all information of an employee Information of from the Table ‘EmployeeInfo’ according to supplied Employee ID><br />-- =============================================<br />Create Proc SP_FetchSpecificEmployeeInfo<br />(<br />EmpIdverchar(50)<br />)<br />As<br /> Begin<br /> Select * from EmployeeInfo where EmployeeID= EmpId<br /> End <br />
  15. 15. Syntax Of Store ProcedureWith out Parameter<br />-- =============================================<br />-- Author: <Author,,Name><br />-- Create date: <Create Date,,><br />-- Description:<Description,,><br />-- =============================================<br />Create Proc SP_Name<br /> As<br /> Begin<br /> Body Of Store Procedure<br /> End<br />
  16. 16. Implemetaion<br />-- =============================================<br /> -- Author: <Author: XYZ><br /> -- Create date: <Create Date:23/06/2010><br /> -- Description: < Used to retrieve all information of the table ‘EmployeeInfo’ without any Condition><br />-- =============================================<br />Create Proc SP_FetchAllEmployeeInfo<br /> As<br /> Begin<br /> Select * from EmployeeInfo<br /> End<br />
  17. 17. Using SP How To Insert Information<br />-- =============================================<br /> -- Author: < XYZ><br /> -- Create date: <23/06/2010><br /> -- Description: < Used To Insert Employee Information ><br />-- =============================================<br />CREATE proc SP_InsertEmployeeInfo<br />(<br /> @ID varchar(20) ,<br /> @Name varchar (50) ,<br /> @Pin varchar(20), <br /> @Email varchar(50), <br /> @MobileNovarchar(20),<br /> @PhoneNovarchar(20),<br /> @MailingAddrvarchar(500) ,<br /> @ParmanentAddrVarchar(500) ,<br /> @Sex varchar(10) ,<br /> @JoingDatesmalldatetime ,<br /> @Post varchar(50) <br />)<br />
  18. 18. AS<br /> Begin<br /> if exists(Select ID From EmployeeInfo Where ID = @ID)<br /> Begin<br /> return 0<br /> End<br /> else <br /> Begin<br /> Insert into EmployeeInfo<br /> (<br /> ID <br /> ,Name <br /> ,Pin <br /> ,Email <br /> ,MobileNo<br /> ,PhoneNo<br /> ,MailingAddr<br /> ,ParmanentAddr<br /> ,Sex <br /> ,JoingDate<br /> ,Post <br /> )<br />
  19. 19. values<br /> (<br /> @ID <br /> ,@Name <br /> ,@Pin <br /> ,@Email <br /> ,@MobileNo<br /> ,@PhoneNo<br /> ,@MailingAddr<br /> ,@ParmanentAddr<br /> ,@Sex <br /> ,@JoingDate<br /> ,@Post<br /> )<br /> End<br /> Begin<br /> return 1<br /> End <br /> End<br />
  20. 20. Using SP How To Update Information<br />-- =============================================<br /> -- Author: < XYZ><br /> -- Create date: <3/06/2010><br /> -- Description: <Used To Update Record Of EmployeeInfo Table ><br />-- =============================================<br />CREATE proc SP_UpdateEmployeeInfo<br />(<br /> @ID varchar(20) ,<br /> @Name varchar (50) ,<br /> @Pin varchar(20), <br /> @Email varchar(50), <br /> @MobileNovarchar(20),<br /> @PhoneNovarchar(20),<br /> @MailingAddrvarchar(500) ,<br /> @ParmanentAddrVarchar(500) ,<br /> @Sex varchar(10) ,<br /> @JoingDatesmalldatetime ,<br /> @Post varchar(50) <br />)<br />
  21. 21. AS<br /> Begin<br /> if not exists(select id from EmployeeInfo where ID = @ID)<br /> Begin <br /> return 0<br /> End <br /> else <br /> Begin <br /> Update EmployeeInfo<br /> set <br /> Name =@Name <br /> ,Pin =@Pin <br /> ,Email =@Email <br /> ,MobileNo =@MobileNo<br /> ,PhoneNo =@PhoneNo<br /> ,MailingAddr = @MailingAddr<br /> ,ParmanentAddr =@ParmanentAddr<br /> ,Sex =@Sex<br /> ,JoingDate =@JoingDate<br /> ,Post =@Post<br /> where ID =@ID <br /> return 1 <br /> End <br /> End <br />
  22. 22. Using SP How TO Delete Record <br />-- =============================================<br /> -- Author: < XYZ><br /> -- Create date: <3/06/2010><br /> -- Description: <Used To Delete Record From EmployeeInfo Table ><br />-- =============================================<br />CREATE PROC SP_DeleteEmployeeInfo<br />(<br /> @id varchar(20)<br />)<br />AS<br /> Begin<br /> if not exists(select id from EmployeeInfo where id = @id)<br /> Begin <br /> return 0<br /> End<br /> else<br />
  23. 23. Begin<br /> if exists(select * from EmployeeInfo where id = @id and DeleteStatus = 0)<br /> Begin<br /> return 1<br /> End<br /> else<br /> Begin<br /> delete from EmployeeInfo where id = @id<br /> return 2<br /> End<br /> End <br />End<br />
  24. 24. How To Execute SP<br /><ul><li>If in a SP , there is no parameter then it executes the following way.
  25. 25. Syntax</li></ul> EXEC SP_NAME <br /><ul><li>Implementation</li></ul> EXEC SP_FetchAllEmployeeInfo<br /><ul><li>If in a SP, there is one or more parameters then it executes the following way.
  26. 26. Syntax
  27. 27. EXEC SP_NAME PARAMETER_lIST
  28. 28. Implementation</li></ul> EXEC SP_FetchAllEmployeeInfo ‘001’<br />
  29. 29. How To Perform Execute SP<br />
  30. 30. sysobjects<br />Name, type, etc. <br />syscomments<br />Text of object<br />syscolumns<br />Parameter list<br />sysdepEnds<br />Object depEndencies<br />Parsing<br />Creation<br />Resolution<br />Resolution*<br />Execution(first timeor recompile)<br />Optimization<br /> Compiled plan placed inunified cache<br />Compilation<br />Processing of Stored Procedures<br />
  31. 31. Initial Steps<br /><ul><li>When a stored procedure is created all objects referenced are resolved (checked to see whether or not they exist).
  32. 32. The create will succeed even if the objects do not exist
  33. 33. Procedures called that do not exist generate error</li></ul> Cannot add rows to sysdepEnds for the current stored procedure because it depEnds on the missing object 'missingobjectname'. The stored procedure will still be created.<br /> Benefit: Recursion is allowed!<br />
  34. 34. Storing and Executing Procedures<br /> When you create a stored procedure, the server stores the text of the procedure in the syscomments table of the current database. In addition, form of the procedure, called a query tree , in the sysprocedures table. The server uses the query tree to create a query plan and then execute.<br />
  35. 35. Building the Query Tree: Resolution<br />The process of building a query tree is called resolution. This process parses the SQL into a more efficient format and resolves all objects representations. <br />For example, table names are resolved into their object IDs and column names are resolved into their column IDs.<br />
  36. 36. Building the Query Plan: Compilation<br /> The process of building a query plan is called compilation. Adaptive Server builds a query plan on the first execution of a stored procedure. Adaptive Server reads the corresponding query tree from the sysprocedures table and loads it into the procedure cache. The server then creates procedure cache. The query plan is the optimized data access path that Adaptive Server uses to execute the procedure.<br /> Adaptive Server determines the optimal data access path and builds the query plan based on the following information:<br /><ul><li>The SQL stored in the query tree
  37. 37. Statistics for each table and index referenced • in the procedure</li></ul>•The values of any parameters passed to the procedure on the first execution.<br />
  38. 38. Multiple Users and the Query Plan<br />Stored procedures are reusable, not reentrant. This means that only one user at a time can execute a given copy of a procedure's query plan. the same procedure at the same time, Adaptive Server creates an additional query plan based on the parameters used in the later execution.<br /> procedure, the query plan is available in cache for reuse by anyone with execute permissions.<br /> If the server must generate a second query plan for a stored procedure, there is no guarantee that it is the same as the first.<br />
  39. 39. Advantage<br /><ul><li>Advantage 1: Stored procedures are modular
  40. 40. Advantage 2: Stored procedures are tunable
  41. 41. Stored procedures are usually written by database developers/administrators.</li></li></ul><li>Thanks <br />

×