Solved

Cursor Syntax

Posted on 2008-10-02
5
624 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
[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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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