Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cursor Syntax

Posted on 2008-10-02
5
Medium Priority
?
644 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:Nathan Riley
[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 2000 total points
ID: 22629005
No that means it will populate the cursor with the first record.
0
 
LVL 12

Author Comment

by:Nathan Riley
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 12

Author Comment

by:Nathan Riley
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 12

Author Comment

by:Nathan Riley
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

604 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