Thursday, 14 May 2015

Creating store procedure Mysql Insert

Showing you some examples of creating store procedure in mysql.
1. "At The Rate" sign used for local variable declare.
2. For calling a store procedure use function below. i am using Codeigniter.
 public function AddBooking(){
  $sql="call usp_AddBooking()";
 $parameters=array();
 $query = $this->db->query($sql,$parameters);
 return $query->result();
 }
 DROP PROCEDURE IF EXISTS `usp_AddBooking` $$
 CREATE PROCEDURE `usp_AddBooking`(
 IN iVendorId INT,
 IN iCustomerId INT, 
 IN iServiceId INT,
 IN dServiceBookingTime DATETIME,
 IN dArrivalTime DATETIME
 )
BEGIN
 DECLARE iBookingId INT default 0;
 BEGIN
 -- ERROR
 set ErrorCode = -999;
 rollback;
 END;
 DECLARE exit handler for sqlwarning
 BEGIN
 -- WARNING
 set ErrorCode = -888;
 rollback;
 END;
 START TRANSACTION;
 INSERT INTO skin_booking 
 (iVendorId 
 ,iCustomerId 
 ,iServiceId
 ,dServiceBookingTime 
 ,dArrivalTime
  ,eStatus
 ,dCreatedDate 
 ,iBookingId 
 ) 
 VALUES 
 (iVendorId 
 ,iCustomerId 
 ,iServiceId 
 ,dServiceBookingTime 
 ,dArrivalTime
 ,'1'
 ,NOW()
 ,iBookingId 
 );
 SET @iBookingId = LAST_INSERT_ID();
 SELECT @iBookingId as BookingId;
 COMMIT;
 END$$

No comments: