Nathan Riley
asked on
Cursor Syntax
Wondering if the where the Cursor is opened it says fetch next from C1 does that mean it will skip the first record of data?
OPEN C1
FETCH NEXT FROM C1
INTO @AccountNo, @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName
*****CODE******
FETCH NEXT FROM C1
INTO @AccountNo, @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoState, @CoZip, @Phone1, @Phone2, @Type, @DebtorID, @StatusID, @OrgID
END
CLOSE C1
DEALLOCATE C1
SET NOCOUNT OFF
RETURN
GO
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
May I ask why you are using a cursor? It should be VERY rare that you ever use a cursor in SQL.
ASKER
It's for populating data into a file that letters are printed from. About 20k records or so in each file processed. Everything from SQL that I have learned was from asking questions here or teaching myself. So is there a better way?
ASKER
This is actually the whole procedure if it makes more sense.
/******************************************************************************
Purpose: Create the PayPlan Letter File
Maintenance History:
Date: Name: Action:
10/02/2008 Nathan Created
******************************************************************************/
CREATE PROCEDURE [dbo].[p_util_PaymentLetter_Out] AS
SET NOCOUNT ON
--Table Variables
declare @LastName varchar(255)
declare @FirstName varchar(255)
declare @AccountNo varchar(255)
declare @Address1 varchar(255)
declare @City varchar(255)
declare @State varchar(255)
declare @Zip varchar(255)
declare @payplanbalance money
declare @InvoiceDate datetime
declare @PaymentAmt money
declare @FirstPayDate datetime
declare @OrgID int
declare @CoName varchar(255)
declare @CoAddress varchar(255)
declare @CoCity varchar(255)
declare @CoState varchar(255)
declare @CoZip varchar(255)
declare @Phone1 varchar(255)
declare @Phone2 varchar(255)
declare @Type varchar(255)
declare @Today datetime
declare @DebtorID int
declare @EventDesc varchar(255)
declare @StatusID int
update debtorpayplan
set PayPlanBalance = case when getdate() > FirstPayDate then Payplanbalance + PaymentAmt end
truncate table letterpayment
declare C1 cursor for
select ltrim(rtrim(D.AccountNo)) as AccountNo,
ltrim(rtrim(D.LastName)) as LastName,
ltrim(rtrim(D.FirstName)) as FirstName,
ltrim(rtrim(A.Address1)) as Address1,
ltrim(rtrim(A.City)) as City,
ltrim(rtrim(A.StateID)) as State,
ltrim(rtrim(A.PostalCode)) as PostalCode,
DP.PayPlanBalance as PayPlanBalance,
cast(DP.LastTermsUpdateDate as datetime) as InvoiceDate,
cast(DP.FirstPayDate as datetime) as FirstPayDate,
DP.PaymentAmt,
BUI.ReturnAddr1 as CoName,
BUI.ReturnAddr2 as CoAddress,
BUI.ReturnCity as CoCity,
BUI.ReturnState as CoState,
BUI.ReturnZip as CoZip,
BUI.BUInvoicePhone1 as Phone1,
BUI.BUInvoicePhone2 as Phone2,
PPT.PayPlanTypeID,
DP.DebtorID,
D.StatusID,
D.OrgID
from Address A
left outer join DebtorAddress DA on A.AddressID = DA.AddressID
right outer join Debtor D on DA.DebtorID = D.DebtorID
left outer join DebtorService DS on D.DebtorID = DS.DebtorID
left outer join DebtorInvoice DI on DS.ServiceID = DI.ServiceID
right outer join DebtorPayPlan DP on D.DebtorID= DP.DebtorID
left outer join Organization O on D.OrgID = O.OrgID
left outer join Account ACC on O.AccountID = ACC.AccountID
left outer join BusinessUnit BU on ACC.AccountID = BU.AccountID
left outer join BusinessUnitInvoice BUI on BU.UnitID = BUI.UnitID
left outer join PayPlanType PPT on DP.PayPlanTypeID = PPT.PayPlanTypeID
where D.StatusID in (10,12,20)
group by
D.OrgID,
DP.DebtorID,
D.AccountNo,
D.LastName,
D.FirstName,
A.Address1,
A.City,
A.PostalCode,
A.StateID,
DP.PayPlanBalance,
DP.LastTermsUpdateDate,
DP.FirstPayDate,
DP.PaymentAmt,
BUI.ReturnAddr1,
BUI.ReturnAddr2,
BUI.ReturnCity,
BUI.ReturnState,
BUI.ReturnZip,
BUI.BUInvoicePhone1,
BUI.BUInvoicePhone2,
DP.PaymentAmt,
D.StatusID,
PPT.PayPlanTypeID
OPEN C1
FETCH NEXT FROM C1
INTO @AccountNo, @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoState, @CoZip, @Phone1, @Phone2, @Type, @DebtorID, @StatusID, @OrgID
WHILE @@FETCH_STATUS = 0
BEGIN
if @Type = '1' and @statusid = 10
BEGIN
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Reminder Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Reminder Letter'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 26, 10, @Today, ' ', 65, @Today, 1, @EventDesc
END
if @Type = '2'
BEGIN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--On Time Payments
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 10 and @PayPlanBalance < 2*@PaymentAmt
begin
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Reminder Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Reminder Letter'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 26, 10, @Today, ' ', 65, @Today, 1, @EventDesc
end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Monthly Payment Made on Account that is in Late Status
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 12 and @PayPlanBalance < 2*@PaymentAmt
begin
--StatusID Change
update debtor
set statusID = 10
where DebtorID = @DebtorID
and OrgID = @OrgID
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Reminder Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Reminder Letter Sent Account back to Current'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 26, 10, @Today, ' ', 65, @Today, 1, @EventDesc
end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Account Going to Late Payments
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 10 and @PayPlanBalance >= 2*@PaymentAmt
begin
--StatusID Change
update Debtor
set StatusID = 12
where DebtorID = @DebtorID
and OrgID = @OrgID
--Status Changed Event
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 10, 12, @Today, ' ', 65, @Today, 1
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Late Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Late Letter Sent'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 27, 12, @Today, ' ', 66, @Today, 1, @EventDesc
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Payment Breach
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 12 and @PayPlanBalance >= 3*@PaymentAmt
begin
--StatusID Change
update Debtor
set StatusID = 20
where DebtorID = @DebtorID
and OrgID = @OrgID
--Status Changed Event
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID,10 ,20 , @Today, ' ', 66, @Today, 1
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Breach Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Breach Letter Sent'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 28, 20, @Today, ' ', 67, @Today, 1, @EventDesc
end
END
if @Type = 3
BEGIN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--On Time Payments
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 10 and @PayPlanBalance < @PaymentAmt
begin
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Balance Reminder', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Balance Reminder Sent'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 26, 10, @Today, ' ', 65, @Today, 1, @EventDesc
end
if @StatusID = 12 and @PayPlanBalance < @PaymentAmt
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Monthly Payment Made on Account that is in Late Status
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
begin
--StatusID Change
update Debtor
set StatusID = 10
where DebtorID = @DebtorID
and OrgID = @OrgID
--Status Changed Event
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 10, 10, @Today, ' ', 66, @Today, 1
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Balance Reminder', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Balance Reminder Sent'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID,26 , 10, @Today, ' ', 65, @Today, 1, @EventDesc
end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Account Going to Late Payments
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 10 and @PayPlanBalance > @PaymentAmt
begin
--StatusID Change
update Debtor
set StatusID = 12
where DebtorID = @DebtorID
and OrgID = @OrgID
--Status Changed Event
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 10, 12, @Today, ' ', 65, @Today, 1
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt, CoName,
CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Late Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Late Letter Sent'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 27, 12, @Today, ' ', 66, @Today, 1, @EventDesc
end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Payment Breach
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if @StatusID = 12 and @PayPlanBalance >= 2*@PaymentAmt
begin
--StatusID Change
update Debtor
set StatusID = 20
where DebtorID = @DebtorID
and OrgID = @OrgID
--Status Changed Event
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID,10 ,20 , @Today, ' ', 66, @Today, 1
--insert letter
insert into LetterPayment(AccountNo, LetterDescription, LastName, FirstName, Address1, City, State, Zip, ServiceAmount, InvoiceDate, FirstPayDate, PaymentAmt,
CoName, CoAddress, CoCity, CoZip, Phone1, Phone2)
values(@AccountNo, 'Breach Letter', @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt,
@CoName, @CoAddress, @CoCity, @CoZip, @Phone1, @Phone2)
--update DebtorEvent
set @EventDesc = 'Breach Letter Sent'
set @Today = getdate()
exec P_INS_DEBTOREVENT @DebtorID, 28, 20, @Today, ' ', 67, @Today, 1, @EventDesc
end
END
FETCH NEXT FROM C1
INTO @AccountNo, @LastName, @FirstName, @Address1, @City, @State, @Zip, @PayPlanBalance, @InvoiceDate, @FirstPayDate, @PaymentAmt, @CoName,
@CoAddress, @CoCity, @CoState, @CoZip, @Phone1, @Phone2, @Type, @DebtorID, @StatusID, @OrgID
END
CLOSE C1
DEALLOCATE C1
SET NOCOUNT OFF
RETURN
GO
ASKER