Advertisement

06.19.2008 at 08:38AM PDT, ID: 23499318
[x]
Attachment Details

A cursor with the name 'tmpCursor' already exists.

Asked by bfowler2 in SQL Server 2005

I am trying to modify a stored procedure that originally passed in a variable from an IIS application.  Instead of passing in a client ID, I want to cycle through all client ID and then make updates based on different criteria.  The challenge that I have is that I receive "A cursor with the name 'tmpCursor' already exists" when I try to run the stored procedure and it creates an infinite loop between @@fetch_staus and declaring the temp  cursor.  I have also tried moving the start of the while statement before the declare tmpClientCursor... line, but another retrieving antother fetch_status further down in the code changes the value to a -1 and the while exits to early.  Any ideas on how to use two while statements and have the script cycle through both?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
ALTER procedure [dbo].[sp_ues_workflow_one_update_address_relationship]  --(@clientid varchar(32))
as
declare
	@currentDate datetime,
	@dateEstablished datetime,
	@dateDifference integer,
	@addressCount integer,
	@currentAddress varchar(20),
	@projectStartDate datetime,
	@projectStart_DateDifference integer,
	@addressRelationship varchar(100),
	@salesAccountType varchar(100),
	@tmp varchar(500),
	@createDate datetime,
	@clientid varchar(32)
	
	
	SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
 begin
 
	set @currentDate = getDate()
	
	
 
	begin
	
	declare tmpClientCursor cursor for 
		select clientid from claddress
	
	open tmpClientCursor
	fetch next from tmpClientCursor into @clientID
	while @@fetch_status = 0 
	--determine that if the current client does not have a sales account type of regular then exit the stored proc
	
	
	declare tmpCursor cursor for 
		select custSalesAccountType from clientCustomTabFields Where clientID = @clientID
			
	open tmpCursor
	fetch from tmpCursor into @salesAccountType
	
	close tmpCursor
	deallocate tmpCursor
	
	if @salesAccountType = 'Regular'
	begin
		--loop through all of the addresses for the current client id
		declare addressCursor cursor for 
			select address from clAddress Where clientID = @clientID
			
		open addressCursor
		fetch from addressCursor into @currentAddress
		
		while @@fetch_status = 0
		begin
		
				declare dateEstablishedCursor cursor for 
				Select IsNull(dateEstablished, '01/01/1901') dateEstablished, isNull(addressRelationship, '') addressRelationship From clAddress Where ClientID = @clientID And Address = @currentAddress
 
				open dateEstablishedCursor
				fetch from dateEstablishedCursor Into @dateEstablished, @addressRelationship
				
				close dateEstablishedCursor
				deallocate dateEstablishedCursor
				
				--conditions for stored procedure
				--1. if the client is not associated with any projects then the address relationship is 'Prospect'
				--2. if the client's address relationship is 'Prospect' the only way the it can change is a change to 'New'
				--3. if a client's address relationship is 'New' the only that it can change is 'existing'
						--a.  however, it could be associated with new projects based upon the start date of the project.  In that case it was stay new and 
						--the date established will be changed to start date of the project
						--b.  if there are no new project's associated with the client then Address Relationship changes to 'Existing' and Date Established Changes to the date the stored proc runs
				--4. if a client's address relationship is 'Existing' then the only way that it can change is to go to 'Former' or go to 'New'
						--a.  the same conditions as three apply here
						
				if @addressRelationship = '' 
				begin	
					print 'Address Relationship is blank'
					--determine if there any projects associated with the client and address
					declare projectCursor cursor for 
					select max(startdate) startdate, max(createdate) from pr where billingclientid = @clientID and clBillingAddr = @currentAddress and wbs2 = '' and wbs3 = '' --will need to ask if this will be clientid or billlingclientid
					
					open projectCursor 
					fetch from projectCursor into @projectStartDate, @createDate
					
					close projectCursor
					deallocate projectCursor
			
					if @projectStartDate is null
					begin
					--change the address relationship to Prospect since there are no projects associated with the client
						update clAddress set AddressRelationship = 'Prospect' where clientID = @clientID and Address = @currentAddress
					end
					else
					begin
						update clAddress set AddressRelationship = 'New', DateEstablished = @createDate where clientID = @clientID and Address = @currentAddress
					end
				end
				
				
				if @addressRelationship = 'Prospect'
				begin
					--if the client's address relationship is Prospect then determine if the client is associated with project 
					--if so then change address relationship to new and date established to createdate of the Project
				
					declare projectCursor cursor for 
					select max(startdate) startdate, max(createdate) createdate  from pr where billingclientid = @clientID and clBillingAddr = @currentAddress and wbs2 = '' and wbs3 = '' --will need to ask if this will be clientid or billlingclientid
					
					open projectCursor 
					fetch from projectCursor into @projectStartDate, @createDate
					
					close projectCursor
					deallocate projectCursor
			
					if @createDate is not null
					begin
					--change the address relationship to Prospect since there are no projects associated with the client
						update clAddress set AddressRelationship = 'New', DateEstablished = @createDate where clientID = @clientID and Address = @currentAddress
					end
				
				end 
				
				if @addressRelationship = 'New'
				begin
					--if the address relationship is 'New' then the only way that it can change is go to existing or stay new
					declare projectCursor cursor for 
					select max(startdate) startdate, max(createdate) createdate  from pr where billingclientid = @clientID and clBillingAddr = @currentAddress and wbs2 = '' and wbs3 = '' --will need to ask if this will be clientid or billlingclientid
					
					open projectCursor 
					fetch from projectCursor into @projectStartDate, @createDate
					
					close projectCursor
					deallocate projectCursor
				
					set @dateDifference = datediff(dd, @projectStartDate, @currentDate)
						
					if @dateDifference >= 365 
						update clAddress set AddressRelationship = 'Existing', DateEstablished = @currentDate where clientID = @clientID and Address = @currentAddress
						
					if @dateDifference < 365
						update clAddress set AddressRelationship = 'New', DateEstablished = @projectStartDate where clientID = @clientID and Address = @currentAddress
				
				
				end
				
				if @addressRelationship = 'Existing'
				begin
					--An existing address relationship can either go to 'Former' or go to 'New'
					declare projectCursor cursor for 
					select max(startdate) startdate, max(createdate) createdate  from pr where billingclientid = @clientID and clBillingAddr = @currentAddress and wbs2 = '' and wbs3 = '' --will need to ask if this will be clientid or billlingclientid
					
					open projectCursor 
					fetch from projectCursor into @projectStartDate, @createDate
					
					close projectCursor
					deallocate projectCursor
				
					set @dateDifference = datediff(dd, @projectStartDate, @currentDate)
						
					if @dateDifference >= 365 
						update clAddress set AddressRelationship = 'Former', DateEstablished = @currentDate where clientID = @clientID and Address = @currentAddress
						
					if @dateDifference < 365
						update clAddress set AddressRelationship = 'New', DateEstablished = @projectStartDate where clientID = @clientID and Address = @currentAddress
				end
				
				if @addressRelationship = 'Former'
				begin
					--An existing address relationship can either go to 'Former' or go to 'New'
					declare projectCursor cursor for 
					select max(startdate) startdate, max(createdate) createdate  from pr where billingclientid = @clientID and clBillingAddr = @currentAddress and wbs2 = '' and wbs3 = '' --will need to ask if this will be clientid or billlingclientid
					
					open projectCursor 
					fetch from projectCursor into @projectStartDate, @createDate
					
					close projectCursor
					deallocate projectCursor
				
					set @dateDifference = datediff(dd, @projectStartDate, @currentDate)
						
					if @dateDifference < 365
						update clAddress set AddressRelationship = 'New', DateEstablished = @projectStartDate where clientID = @clientID and Address = @currentAddress
				end
			fetch next from addressCursor Into @currentAddress	
		end -- end of while statement
		
		close addressCursor
		deallocate addressCursor
		
	end --end statement for sales account type
		end
	close tmpClientCursor
	deallocate tmpClientCursor
end
[+][-]06.19.2008 at 08:55AM PDT, ID: 21823491

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Server 2005
Sign Up Now!
Solution Provided By: ruscomp
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.19.2008 at 08:57AM PDT, ID: 21823511

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 09:34AM PDT, ID: 21823893

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.19.2008 at 10:19AM PDT, ID: 21824289

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 10:28AM PDT, ID: 21824394

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 10:31AM PDT, ID: 21824427

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 10:34AM PDT, ID: 21824449

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 10:59AM PDT, ID: 21824701

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 11:07AM PDT, ID: 21824766

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 11:07AM PDT, ID: 21824771

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 11:24AM PDT, ID: 21824911

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 11:31AM PDT, ID: 21824980

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 11:57AM PDT, ID: 21825259

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 12:01PM PDT, ID: 21825295

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 12:10PM PDT, ID: 21825402

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628