SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored procedure to transfer Applications into Loans mismatching number of BEGIN and COMMIT...


Stored procedure to transfer Applications into Loans mismatching number of BEGIN and COMMIT statementserror:

Author
Message
angel.alvarado.colon
angel.alvarado.colon
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 2
I had a problem when transfering an active application and to a loan table where i have to calculate the monthly payment.
When i try to execute the stored procedure i get this error:
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1

Here is the code use in the procedure. When i executed is says commited succesfully. So i dont know why i get this message. I tried several things i seen on google but this getting this annoying message. I will very thankfull if someone can help me with this.

ALTER Procedure [dbo].[Transfer_ApprovedApp_intoLoans]
@FirstPaymentDate as Date

AS

Begin
Begin Transaction
DECLARE @RowCount As Int;
DECLARE @ApplicationID as char(5);
DECLARE @LoanTypeID as char(5);
DECLARE @LoanAmount As Numeric(8,4);
DECLARE @LoanRate As Numeric(12,8);
DECLARE @LoanYears As Int;
DECLARE @NumberOfMonths As Int = 12; /**Number of months**/
DECLARE @MonthlyRate As Numeric(12,8); /**To use to convert the anual rate to monthly rate **/
DECLARE @NumberOfPeriods As Int; /**Months multiplied by the number of years**/
DECLARE @MonthlyPayment As Numeric(8,4);
DECLARE @RateinPercent AS Integer = 100
DECLARE @Status AS Char(1)


/**Check if application is active and approved**/
SELECT *
FROM Applications
WHERE ApplicationID = @ApplicationID AND Status = 'A'

/**Check if Loan exists**/
SELECT @RowCount = Count(*)
FROM Applications
WHERE ApplicationID = @ApplicationID
IF @RowCount = 0
BEGIN
PRINT 'Applicación' + @ApplicationID + ' no está aprobada o activa';
Return;
END;
Else

/**Declare cursor**/
Declare AppTransferLoanCursor CURSOR for
Select ApplicationId,LoanTypeId,LoanAmount,LoanRate,LoanYears, [Status]
From Applications
Where Status = 'A' and Transferred = 'N';

/** Open Cursor **/
Open AppTransferLoanCursor
Fetch Next from AppTransferLoanCursor Into @ApplicationID, @LoanTypeID, @LoanAmount,
@LoanRate, @LoanYears, @Status
While @@FETCH_STATUS = 0


Begin
/**Calculate monthly payment**/
Set @MonthlyRate =(@LoanRate/@RateinPercent)/@NumberOfMonths
Set @NumberOfPeriods = @LoanYears* @NumberOfMonths
Set @MonthlyPayment=@LoanAmount/(power(1+@LoanRate,@NumberOfPeriods)-1)*(@LoanRate*power(1+@LoanRate,@NumberOfPeriods))

/*Mark the application as transferred**/
Update Applications
Set Transferred = 'Y'
Where ApplicationId = @ApplicationId


/**Transfer values into Loans**/
Insert into Loans(LoanType,LoanAmount,LoanRate,LoanYears,MonthlyPayment,Status)
Values(@LoanTypeID,@LoanAmount,@LoanRate,@LoanYears,@MonthlyPayment, @Status)

/**To fetch next row**/
Fetch Next from AppTransferLoanCursor Into @ApplicationID, @LoanTypeID, @LoanAmount,
@LoanRate, @LoanYears, @Status
End
Commit Transaction
Close ApplicationTransferCursor
DEALLOCATE ApplicationTransferCursor


End
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41126 Visits: 20000

...
/**Check if Loan exists**/
SELECT @RowCount = Count(*)
FROM Applications
WHERE ApplicationID = @ApplicationID

IF @RowCount = 0
BEGIN
PRINT 'Applicación' + @ApplicationID + ' no está aprobada o activa';
ROLLBACK TRANSACTION -- complete the transaction before exiting the stored procedure
Return;
END;
Else
...



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41126 Visits: 20000
You should consider replacing the slow slow cursor with a fast set-based equivalent:

UPDATE a SET 
Transferred = 'Y'
OUTPUT
a.LoanTypeId,
a.LoanAmount,
a.LoanRate,
a.LoanYears,
MonthlyPayment = a.LoanAmount/(y.Result-1) * (a.LoanRate*y.Result),
a.[Status]
INTO Loans
FROM Applications a
CROSS APPLY (SELECT NumberOfPeriods = a.LoanYears * @NumberOfMonths) x
CROSS APPLY (SELECT Result = power(1+a.LoanRate,x.NumberOfPeriods)) y
WHERE a.[Status] = 'A'
AND a.Transferred = 'N';



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search