Solved

Cursor Syntax

Posted on 2008-10-02
5
606 Views
Last Modified: 2012-05-05
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

Open in new window

0
Comment
Question by:N R
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
aaronakin earned 500 total points
ID: 22629005
No that means it will populate the cursor with the first record.
0
 
LVL 11

Author Comment

by:N R
ID: 22629009
ok thanks just wanted to be sure.
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22629015
May I ask why you are using a cursor?  It should be VERY rare that you ever use a cursor in SQL.
0
 
LVL 11

Author Comment

by:N R
ID: 22629030
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?
0
 
LVL 11

Author Comment

by:N R
ID: 22629050
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

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

786 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