7. What is store procedure? A stored procedure is a set of SQL commands that has been compiled and stored on the database server. 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. Stored procedures improve performance by reducing network traffic and CPU load.
13. Syntax Of Store ProcedureWith Parameter -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description:<Description,,> -- ============================================= Create Proc SP_Name ( Param1 DataType, param2 DataType, Param3 DataType, . . . ParamnDataType ) As Begin Body Of Store Procedure End
14. Implementation: -- ============================================= -- Author: <Author: XYZ> -- Create date: <Create Date:23/06/2010> -- Description: < Used to retrieve all information of an employee Information of from the Table ‘EmployeeInfo’ according to supplied Employee ID> -- ============================================= Create Proc SP_FetchSpecificEmployeeInfo ( EmpIdverchar(50) ) As Begin Select * from EmployeeInfo where EmployeeID= EmpId End
15. Syntax Of Store ProcedureWith out Parameter -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description:<Description,,> -- ============================================= Create Proc SP_Name As Begin Body Of Store Procedure End
16. Implemetaion -- ============================================= -- Author: <Author: XYZ> -- Create date: <Create Date:23/06/2010> -- Description: < Used to retrieve all information of the table ‘EmployeeInfo’ without any Condition> -- ============================================= Create Proc SP_FetchAllEmployeeInfo As Begin Select * from EmployeeInfo End
17. Using SP How To Insert Information -- ============================================= -- Author: < XYZ> -- Create date: <23/06/2010> -- Description: < Used To Insert Employee Information > -- ============================================= CREATE proc SP_InsertEmployeeInfo ( @ID varchar(20) , @Name varchar (50) , @Pin varchar(20), @Email varchar(50), @MobileNovarchar(20), @PhoneNovarchar(20), @MailingAddrvarchar(500) , @ParmanentAddrVarchar(500) , @Sex varchar(10) , @JoingDatesmalldatetime , @Post varchar(50) )
18. AS Begin if exists(Select ID From EmployeeInfo Where ID = @ID) Begin return 0 End else Begin Insert into EmployeeInfo ( ID ,Name ,Pin ,Email ,MobileNo ,PhoneNo ,MailingAddr ,ParmanentAddr ,Sex ,JoingDate ,Post )
19. values ( @ID ,@Name ,@Pin ,@Email ,@MobileNo ,@PhoneNo ,@MailingAddr ,@ParmanentAddr ,@Sex ,@JoingDate ,@Post ) End Begin return 1 End End
20. Using SP How To Update Information -- ============================================= -- Author: < XYZ> -- Create date: <3/06/2010> -- Description: <Used To Update Record Of EmployeeInfo Table > -- ============================================= CREATE proc SP_UpdateEmployeeInfo ( @ID varchar(20) , @Name varchar (50) , @Pin varchar(20), @Email varchar(50), @MobileNovarchar(20), @PhoneNovarchar(20), @MailingAddrvarchar(500) , @ParmanentAddrVarchar(500) , @Sex varchar(10) , @JoingDatesmalldatetime , @Post varchar(50) )
21. AS Begin if not exists(select id from EmployeeInfo where ID = @ID) Begin return 0 End else Begin Update EmployeeInfo set Name =@Name ,Pin =@Pin ,Email =@Email ,MobileNo =@MobileNo ,PhoneNo =@PhoneNo ,MailingAddr = @MailingAddr ,ParmanentAddr =@ParmanentAddr ,Sex =@Sex ,JoingDate =@JoingDate ,Post =@Post where ID =@ID return 1 End End
22. Using SP How TO Delete Record -- ============================================= -- Author: < XYZ> -- Create date: <3/06/2010> -- Description: <Used To Delete Record From EmployeeInfo Table > -- ============================================= CREATE PROC SP_DeleteEmployeeInfo ( @id varchar(20) ) AS Begin if not exists(select id from EmployeeInfo where id = @id) Begin return 0 End else
23. Begin if exists(select * from EmployeeInfo where id = @id and DeleteStatus = 0) Begin return 1 End else Begin delete from EmployeeInfo where id = @id return 2 End End End
33. Procedures called that do not exist generate error 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. Benefit: Recursion is allowed!
34. Storing and Executing Procedures 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.
35. Building the Query Tree: Resolution 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. For example, table names are resolved into their object IDs and column names are resolved into their column IDs.
36.
37. Statistics for each table and index referenced • in the procedure•The values of any parameters passed to the procedure on the first execution.
38. Multiple Users and the Query Plan 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. procedure, the query plan is available in cache for reuse by anyone with execute permissions. 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.