Solved

On Error Rollback to all table

Posted on 2014-01-01
3
220 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
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 backup issue 3 19
Help with SQL Query 23 38
Contained Database Collations 6 18
Auditing with Temporal Tables 4 14
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now