SanjaySutar
Starting Member
18 Posts |
Posted - 2008-10-23 : 03:37:09
|
Hi, i have a stored procedure which is throwing error as Cursor "already exits". The procedure is as follows :set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER Procedure [dbo].[spl_Mass_Status_Bags](@Branch_Code varchar(3),@Company_Code varchar(4),@event_code varchar(2),@office_code varchar (5),@location_code varchar (3),@exception_code varchar (2),@bag_number varchar (11),@flight_number varchar (10),@Date Varchar(15),@remarks varchar(255),@Time varchar(20),@GMT int,@entry_by varchar(10),@data_center varchar (5))AsBegin Begin Trydeclare @Reference_number Varchar(20)Declare @ErrorMessage Varchar(300)Declare @ErrorProc Varchar(50)Declare @shipment_id varchar(18)Declare @tracking_number varchar(20)Declare @GetDateTime varchar(20)Declare @event_type char(1)select Exception_code from Exception where Exception_code=@Exception_codeif(@@RowCount=0) Begin Raiserror('Exception Code does not exists',16,1) return EndSelect Bag_number from bags_in_office Where Bag_number = @Bag_numberif(@@Rowcount>=1) Begin exec sp_nextSerialNumGet @company=@Company_Code,@branch=@branch_code,@division='',@location='',@office='',@serial='X1',@category='',@result=@Reference_number output set @GetDateTime=@Date + ' ' + @Time Insert Into status_header (reference_number,event_code,office_code,location_code,exception_code,bag_number,flight_number,status_datetime,remarks,update_datetime,entry_by,data_center) Values (@Reference_number,@event_code,@office_code,@location_code,@exception_code,@bag_number,@flight_number,@GetDateTime,@remarks,dbo.udf_gmtgetdate(@GMT),@entry_by,@data_center) select @event_type=event_type from event_master where event_code=@event_codeDeclare Fetch_Data CursorFor select shipment_id, tracking_number from bag_detail bd inner join bag_header bh on bh.bag_number = bd.bag_number where bh.bag_number = @bag_numberOpen Fetch_Data Fetch Next From Fetch_Data Into @Shipment_id,@Tracking_NumberIf(@@Fetch_Status=-1) Begin Raiserror('Rows not found',16,1) EndWhile @@Fetch_Status=0 Begin declare @Status_details_id varchar(18) exec sp_nextSerialNumGet @company=@Company_Code,@branch=@branch_code,@division='',@location='',@office='',@serial='X1',@category='',@result=@Status_details_id output Insert into status_detail (status_detail_id,reference_number,shipment_id,tracking_number,status_datetime) Values (@Status_details_id ,@Reference_number,@shipment_id,@tracking_number,@GetDateTime) Declare @tracker_id Varchar(18) Exec sp_nextSerialNumGet @company=@Company_Code,@branch=@branch_code,@division='',@location='',@office='',@serial='X1',@category='',@result=@tracker_id output Insert into package_tracker (tracker_id,event_code,reference_number,event_date_time,shipment_id,tracking_number,event_type,location_code,flight_number,bag_number,description,entry_by,entry_datetime,data_center) Values (@tracker_id,@event_code,@Reference_number,@GetDateTime,@shipment_id,@tracking_number,@event_type,@location_code,NULL,@bag_number,@remarks,@entry_by,dbo.udf_gmtgetdate(@GMT),@data_center) Fetch Next From Fetch_Data Into @Shipment_id,@Tracking_Number End Close Fetch_Data Deallocate Fetch_Data Endelse Begin raiserror('Bag number not found',16,1) EndEnd TryBegin Catch Set @ErrorProc='Procedure ' + ' ' + ERROR_PROCEDURE() Set @ErrorMessage= @ErrorProc + ' ' + ERROR_MESSAGE() Raiserror(@ErrorMessage,16,1)End CatchEnd San |
|