Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

On Error Rollback to all table

Posted on 2014-01-01
3
Medium Priority
?
231 Views
Last Modified: 2014-01-01
Dear Experts,
I have attached a procedure, which is updating three different tables. I want to run rollback statement in case of any error in any procedure to all three tables.
Can some one help me?
FeeGenerateProcedure.sql
0
Comment
Question by:Mehram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39749705
Yes, you can use the try..catch from the start of your procedure as shown below

--Create Procedure CreateFee(
--@MonthNo				int				=	NULL
--, @FeeType				Varchar(100)	=	NULL
--, @StudentId			int				=	NULL
--, @ClassId				int				=	NULL
--, @FeeDiscount			float			=	NULL
--, @CampusId				int				=	NULL
--, @FiscalYearID			int				=	NULL
--, @LastUpdateUser		int				=	NULL
--, @LastUpdateDate		datetime		=	NULL
--)
--as

--select @LastUpdateDate = getdate()
--Declare @FiscalYearId int
--Set @FiscalYearId = (Select FiscalYearId from FiscalYear Where Active = '1')

Declare @MonthNo int
Declare @FeeType Varchar(100)
Declare @CampusId int
Declare @FiscalYearID int
Declare @LastUpdateUser int
Declare @LastUpdateDate DateTime
Declare	@ReceiptNo varchar(50)=NULL
Declare @YearNo	varchar(50) = NULL
Set @MonthNo='4'
Set @CampusId='1'
SET @FeeType = '1,4,5'
Set @FiscalYearID='1'
Set @LastUpdateUser='1'
Set @LastUpdateDate = getdate()
Set @YearNo='1314'

BEGIN TRY
   BEGIN TRAN

---Insert Select Fee Types in Temp Table
Begin
CREATE TABLE #FeeTypeTemp(ID Int Identity(1,1) Not NULL, FeeType INT)
INSERT Into #FeeTypeTemp(FeeType)
SELECT * FROM dbo.SplitIDs(@FeeType)
End


---Insert Into FeeMaster 
Begin
---Insert Into	FeeMaster (MonthNo, StudentId, FeeDiscount, FiscalYearId, CampusId, LastUpdateUser, LastUpdateDate)
Select MonthNo=@MonthNo, StudentId, FeeDiscount, FiscalYearID=@FiscalYearId, CampusId=@CampusId, LastUpdateUser=@LastUpdateUser, LastUpdateDate=getdate()   from Students  
Where IsActive='1'
End


---Insert Into FeeDetail
Begin
--Insert Into	FeeDetail(FeeMasterId, MonthNo, FeeTypeId, FeeAmount, StudentId, CampusId, LastUpdateUser, LastUpdateDate)
Select b.FeeMasterId 
	, b.MonthNo 
	, c.FeeTypeId 
	, FeeAmount=Case When @Campusid='1' and a.FeeTerm='0' then c.FeeAmount
					When @Campusid='1' and a.FeeTerm='1' then c.FeeAmount/2 
					When @Campusid='1' and a.FeeTerm='2' then '0'
					When @Campusid='1' and a.FeeTerm='3' then '0'
					When @Campusid='1' and a.FeeTerm='4' then a.FeeDiscount 
			        
			        When @Campusid='2' and a.FeeTerm='0' then c.FeeAmount 
					When @Campusid='2' and (a.FeeTerm='1' OR a.FeeTerm='3') and c.FeeTypeID='4' and @MonthNo='4' then c.FeeAmount 
					When @Campusid='2' and a.FeeTerm='1' and c.FeeTypeID='4' and @MonthNo<>'4' then c.FeeAmount/2
					When @Campusid='2' and a.FeeTerm='1' and c.FeeTypeID<>'4' and @MonthNo<>'4' then c.FeeAmount
					When @Campusid='2' and a.FeeTerm='2' then '0'
					When @Campusid='2' and a.FeeTerm='3' and c.FeeTypeID='4' and @MonthNo<>'4' then '0'
					When @Campusid='2' and a.FeeTerm='4' and c.FeeTypeID='4' then a.FeeDiscount
			        
			        When @Campusid='3' and a.FeeTerm='0' then c.FeeAmount 
			        When @Campusid='3' and a.FeeTerm='1' then c.FeeAmount/2 
			        When @Campusid='3' and a.FeeTerm='2' then '0' 
					When @Campusid='3' and a.FeeTerm='3' then '0'
					When @Campusid='3' and a.FeeTerm='4' and c.FeeTypeID='4' then a.FeeDiscount 
					When @Campusid='3' and a.FeeTerm='4' and c.FeeTypeID<>'4' then c.FeeAmount  
			   else 0 end 
	, a.StudentId 
	, CampusID=@CampusId
	, LastUpdateUser=@LastUpdateUser
	, LastUpdateDate=@LastUpdateDate
from Students a
Join FeeMaster b on a.StudentId=b.StudentId
Join ClassesFee c on a.PresentClassId=c.ClassId
Join #FeeTypeTemp d on c.FeeTypeId=d.FeeType  
where a.StudentId=b.StudentId 
and b.MonthNo=@MonthNo 
and c.CampusId=@CampusId 
and c.FiscalYearID=@FiscalYearId
End
if not exists (select 1 from FeeTransactions where monthNo = @MonthNo and FiscalYearId=@FiscalYearID and CampusId=@CampusId)
begin
--	Insert Into FeeTransactions
	Select	GetDate(),MonthNo,Debit=0,Credit=Sum(FeeAmount),FeeDiscount=isnull(s.FeeDiscount,0),Descriptions='Fee generated for the month of ' + convert(varchar,MonthNo),DiscountAmount=0,d.StudentId,p.ComputerNo,p.ParentId
				,ReceiptNo=@YearNo+Convert(Varchar,s.Studentid)+convert(varchar,MonthNo),FeeMasterId,FiscalYearId=@FiscalYearId,d.CampusId,LastUpdateUser=@LastUpdateUser,LastUpdateDate=getdate()
				,d.ClassId,d.SectionId,d.ShiftId
	From		FeeDetail d
	Join		Students s on (s.StudentId = d.StudentId)
	left Join	Parents p on (p.ParentId = s.ParentId)
	Where		(MonthNo = @MonthNo)
	and			(s.StudentId not in (Select StudentId from FeeTransactions ft Where ft.MonthNo = @MonthNo and FiscalYearId=@FiscalYearID and CampusId=@CampusId))
	Group by
	MonthNo,d.StudentId,s.FeeDiscount,d.ClassId,d.SectionId,d.ShiftId,s.StudentId,p.ParentId,d.CampusId,FeeMasterId,p.ComputerNo 

	order by studentid
end

Drop Table #FeeTypeTemp
COMMIT TRAN
END TRY
BEGIN CATCH
SELECT 'Error Number = ' + CAST(error_number() AS VARCHAR) + ' Error description ' + error_description()
ROLLBACK 
END CATCH

Open in new window

0
 

Author Comment

by:Mehram
ID: 39749709
Showing error

Msg 195, Level 15, State 10, Line 114
'error_description' is not a recognized built-in function name.

error_description()
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 total points
ID: 39749727
My mistake, it should be error_message instead

--Create Procedure CreateFee(
--@MonthNo				int				=	NULL
--, @FeeType				Varchar(100)	=	NULL
--, @StudentId			int				=	NULL
--, @ClassId				int				=	NULL
--, @FeeDiscount			float			=	NULL
--, @CampusId				int				=	NULL
--, @FiscalYearID			int				=	NULL
--, @LastUpdateUser		int				=	NULL
--, @LastUpdateDate		datetime		=	NULL
--)
--as

--select @LastUpdateDate = getdate()
--Declare @FiscalYearId int
--Set @FiscalYearId = (Select FiscalYearId from FiscalYear Where Active = '1')

Declare @MonthNo int
Declare @FeeType Varchar(100)
Declare @CampusId int
Declare @FiscalYearID int
Declare @LastUpdateUser int
Declare @LastUpdateDate DateTime
Declare	@ReceiptNo varchar(50)=NULL
Declare @YearNo	varchar(50) = NULL
Set @MonthNo='4'
Set @CampusId='1'
SET @FeeType = '1,4,5'
Set @FiscalYearID='1'
Set @LastUpdateUser='1'
Set @LastUpdateDate = getdate()
Set @YearNo='1314'

BEGIN TRY
   BEGIN TRAN

---Insert Select Fee Types in Temp Table
Begin
CREATE TABLE #FeeTypeTemp(ID Int Identity(1,1) Not NULL, FeeType INT)
INSERT Into #FeeTypeTemp(FeeType)
SELECT * FROM dbo.SplitIDs(@FeeType)
End


---Insert Into FeeMaster 
Begin
---Insert Into	FeeMaster (MonthNo, StudentId, FeeDiscount, FiscalYearId, CampusId, LastUpdateUser, LastUpdateDate)
Select MonthNo=@MonthNo, StudentId, FeeDiscount, FiscalYearID=@FiscalYearId, CampusId=@CampusId, LastUpdateUser=@LastUpdateUser, LastUpdateDate=getdate()   from Students  
Where IsActive='1'
End


---Insert Into FeeDetail
Begin
--Insert Into	FeeDetail(FeeMasterId, MonthNo, FeeTypeId, FeeAmount, StudentId, CampusId, LastUpdateUser, LastUpdateDate)
Select b.FeeMasterId 
	, b.MonthNo 
	, c.FeeTypeId 
	, FeeAmount=Case When @Campusid='1' and a.FeeTerm='0' then c.FeeAmount
					When @Campusid='1' and a.FeeTerm='1' then c.FeeAmount/2 
					When @Campusid='1' and a.FeeTerm='2' then '0'
					When @Campusid='1' and a.FeeTerm='3' then '0'
					When @Campusid='1' and a.FeeTerm='4' then a.FeeDiscount 
			        
			        When @Campusid='2' and a.FeeTerm='0' then c.FeeAmount 
					When @Campusid='2' and (a.FeeTerm='1' OR a.FeeTerm='3') and c.FeeTypeID='4' and @MonthNo='4' then c.FeeAmount 
					When @Campusid='2' and a.FeeTerm='1' and c.FeeTypeID='4' and @MonthNo<>'4' then c.FeeAmount/2
					When @Campusid='2' and a.FeeTerm='1' and c.FeeTypeID<>'4' and @MonthNo<>'4' then c.FeeAmount
					When @Campusid='2' and a.FeeTerm='2' then '0'
					When @Campusid='2' and a.FeeTerm='3' and c.FeeTypeID='4' and @MonthNo<>'4' then '0'
					When @Campusid='2' and a.FeeTerm='4' and c.FeeTypeID='4' then a.FeeDiscount
			        
			        When @Campusid='3' and a.FeeTerm='0' then c.FeeAmount 
			        When @Campusid='3' and a.FeeTerm='1' then c.FeeAmount/2 
			        When @Campusid='3' and a.FeeTerm='2' then '0' 
					When @Campusid='3' and a.FeeTerm='3' then '0'
					When @Campusid='3' and a.FeeTerm='4' and c.FeeTypeID='4' then a.FeeDiscount 
					When @Campusid='3' and a.FeeTerm='4' and c.FeeTypeID<>'4' then c.FeeAmount  
			   else 0 end 
	, a.StudentId 
	, CampusID=@CampusId
	, LastUpdateUser=@LastUpdateUser
	, LastUpdateDate=@LastUpdateDate
from Students a
Join FeeMaster b on a.StudentId=b.StudentId
Join ClassesFee c on a.PresentClassId=c.ClassId
Join #FeeTypeTemp d on c.FeeTypeId=d.FeeType  
where a.StudentId=b.StudentId 
and b.MonthNo=@MonthNo 
and c.CampusId=@CampusId 
and c.FiscalYearID=@FiscalYearId
End
if not exists (select 1 from FeeTransactions where monthNo = @MonthNo and FiscalYearId=@FiscalYearID and CampusId=@CampusId)
begin
--	Insert Into FeeTransactions
	Select	GetDate(),MonthNo,Debit=0,Credit=Sum(FeeAmount),FeeDiscount=isnull(s.FeeDiscount,0),Descriptions='Fee generated for the month of ' + convert(varchar,MonthNo),DiscountAmount=0,d.StudentId,p.ComputerNo,p.ParentId
				,ReceiptNo=@YearNo+Convert(Varchar,s.Studentid)+convert(varchar,MonthNo),FeeMasterId,FiscalYearId=@FiscalYearId,d.CampusId,LastUpdateUser=@LastUpdateUser,LastUpdateDate=getdate()
				,d.ClassId,d.SectionId,d.ShiftId
	From		FeeDetail d
	Join		Students s on (s.StudentId = d.StudentId)
	left Join	Parents p on (p.ParentId = s.ParentId)
	Where		(MonthNo = @MonthNo)
	and			(s.StudentId not in (Select StudentId from FeeTransactions ft Where ft.MonthNo = @MonthNo and FiscalYearId=@FiscalYearID and CampusId=@CampusId))
	Group by
	MonthNo,d.StudentId,s.FeeDiscount,d.ClassId,d.SectionId,d.ShiftId,s.StudentId,p.ParentId,d.CampusId,FeeMasterId,p.ComputerNo 

	order by studentid
end

Drop Table #FeeTypeTemp
COMMIT TRAN
END TRY
BEGIN CATCH
SELECT 'Error Number = ' + CAST(error_number() AS VARCHAR) + ' Error description ' + error_message()
ROLLBACK 
END CATCH

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question