Solved

On Error Rollback to all table

Posted on 2014-01-01
3
229 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
union query column need default text 2 22
Amazon RDS migrate to SQL Server 3 50
Cannot get group page break to work in SSRS 2008R2. 14 40
What is needed to become a DBA? 7 56
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 …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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