[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

Cfquery and array problem

I got a little problem here. I have a query and I am trying to build an array to populate all the fields in to the array.

Then i need to  check if the person is in the folowing  list of jobcodes <cfset VPofOps="8074,60851">
If it is do the insert  
The code is below
<cfquery name="qry_getDescription" datasource="xxxxxx">
		Select * from OrgUnitLocation
<!---		inner join Location l on l.OpUnit=OrgUnitLocation.OpUnit--->
where OpUnit='50029'
		order by JobDescription
	</cfquery>
	<cfset myArr = ArrayNew(1)>
	<cfset VPofOps="8074,60851">
	<cfset VPSales ="5655,7080">
	<cfset AreaVP = "8070,8073">
 
	
	<cfset AMofOps = "7072">
	<cfset AMSales = "7073">
	<cfset AMDRC = "1130">
	
	
	<cfloop query="qry_getDescription">
		<cfset myArr[currentrow][1]= OpUnit>
		<cfset myArr[currentrow][2]= Employeeid>
		<cfset myArr[currentrow][3]= First_name>
		<cfset myArr[currentrow][4]= Last_Name>
		<cfset myArr[currentrow][5]= JobDescription>
		<cfset myArr[currentrow][6]= JobCode>
	</cfloop>
	
		<cfloop query="qry_getDescription">
			<cfset lOpUnit = myArr[1]>
			<cfset lEmployeeid = myArr[2]>
			<cfset lFirstname = myArr[3]>
			<cfset lLastName = myArr[4]>
			<cfset lJobDescription = myArr[5]>
			<cfset lJobCode = myArr[6]>
		
		</cfloop>

Open in new window

0
erikTsomik
Asked:
erikTsomik
  • 10
  • 10
1 Solution
 
_agx_Commented:
Which field relates to VPofOps,  lEmployeeid or lJobCode?

I do not see any insert code. Can you explain what you need there? Does it insert have to be done using the arrays, or can you just do a separate query:

<cfquery name="addTable" datasource="xxxx">
        INSERT INTO SomeTable (....)
        SELECT  Columns
        FROM    OrgUnitLocation
        WHERE OpUnit='50029'  
        AND     lEmployeeid  IN ( 8074,60851 )
</cfquery>
0
 
gdemariaCommented:
You are defining a ONE dimensional array

 <cfset myArr = ArrayNew(1)>

But trying to use a TWO dimensional array...

<cfset myArr[currentrow][1]= OpUnit>

I suggest using a structure and array, its a lot clearer to refer to a variable as OpUnit than it is as "1" ...

It would look like this..   I think it's easier...

<cfset myArr[currentrow] = structNew()>
<cfset myArr[currentrow].OpUnit  = OpUnit>
<cfset myArr[currentrow].Employeeid= Employeeid>



Regarding checking the JobCode....

<cfloop query="qry_getDescription">

  <cfset VPofOps="8074,60851">
  <cfif listFind( VPofOps, qry_getDescription.JobCode)>
     .... YES, it's on the list....
  <cfelse>
     ... NO, job code is NOT on the list
  </cfif>



0
 
gdemariaCommented:
Oh, and I agree, however, with agx.   If you can just do it in an INSERT/SELECT query, that would be the easiest way...
0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
erikTsomikSystem Architect, CF programmer Author Commented:
the task that I am trying to achieve is the following . I have a DB which has employeeid,jobCode,jobdescription. What i need to do :

1.Get everybody whose jobCode is 60851 or 8074. and insert into the region1 table.
The same table has columns AVPEmpID and SManagerEmpID.
So the next step is to get SmaNagerEmpID (jobCode is 5655 or 7080) and Update the same Table where regionID is the Identity key from the previous insert.

The sample data:to get people: (for 1 locationID I have over 100. For the sample sake give only 1):
OpUnit EmpID FName Lname JobDescription JobCode
6 1 J D Human Resources Director 30090
6 5 C Z Area Sales Manager 7073
6 7 S K Executive Director 8050
0
 
gdemariaCommented:
> Get everybody whose jobCode is 60851 or 8074. and insert into the region1 table.

This seems like agx's example...

insert into region1 table (columns?)
 select ???  from employee where jobcode in (5655,7080)

> The same table has columns AVPEmpID and SManagerEmpID.

which table?   Region1 or Employees ?

>  So the next step is to get SmaNagerEmpID (jobCode is 5655 or 7080)

From which table?  With what where clause?

> and Update the same Table where regionID is the Identity key from the previous insert.

Update the employee table using the region ID ?  
So you have a foreign key in the region1 pointing to employees and a foreign key in employees pointing to region1?



0
 
gdemariaCommented:
Ok Erik, let me help you ask a question. First, you need to clearly say what your tables look like, then you need to clearly state your objectives in relationship to those tables. I am still trying to put together these two things. So please correct the information below....



This is the (no name) table, so we'll call it EMPLOYEES

EmpID --- this is the primary key
OpUnit
FName
Lname
JobDescription
JobCode


This is the REGION1 table...

-- There is no Primary Key in this table
ManagerEmpID -- this is a foreign key to the Employee Table
SMAnagerEmpID -- this is a foreign key to the Employee Table
AVPEmpID -- this is a foreign key to the employee table


This is the OrgUnitLocation table...

It has this data:
50029 101469 John Smith Director 7523

It has no column names
but that doesn't matter because we don't use this table anyway




----------- Objective -------------

1) I want to insert into REGION1, column ManagerEmpID (I'm just guessing here) the EMPLOYEE ID for all EMPLOYEES where the JOBCODE is either 60851 or 8074.

2) I want update the REGION1 table by setting the SmaNagerEmpID to the same as ManagerEmpID but only for employees who have a job code of either 5655 or 7080

3) I have no idea why I even mentioned the OrgUnitLocation Table. Just thought I would share it...

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
to clarify all this .
I have txt file where from which i upload the data into OrgUnitLocation. THe OrgUnitLocation has the following structure:
OpUnit, Employeeid,First_Name,Last_name,JobDescription,JobCode -- there is no primary keys

There is a region1 table where I need to insert/update the data from the OrgUnitLocation. The region1 table has the following structure:
RegionID-primary key
Name (will consists of last name's of the managerEmpID,SmanagerEmpID,AvpEmpID- ; separated)
ManagerEmpID
SManagerEmpID
AvpEmpID

Objective:
1.First every time it runs I need to delete all data from region1 table
2.Then find people who has jobCodes 8074 or 60851. (ManagerEmpID)
3.find people who has jobCodes 5655 or 7080 (SmanagerEmpID)
4.find people who has jobCodes 8070 or 8073 (AvpEmpID)

The region1 table should looks like this
3948 Jones;A 49858 3735 59195
3949 Flintstone;Rubble; 503638 509838 44443
3950 Smith;Smith;Ri 101484 101480 101484
3951 Slate;Draper;Char 42605 87348 53163

The OrgUnitDate is attached in the file. And the code that i came up with is below




<cfsetting requesttimeout="9000">
<cfquery name="qry_getDescription" datasource="xxxxx">
		Select * from OrgUnitLocation
		where  jobCode in ('8074','60851')
		order by OpUnit
	</cfquery>
	<cfparam name="holdRegionID" default="0">
		<CFQUERY name="EraseAreas"  datasource="XXXXX" >
				Delete  from Area1
			</cfquery>
			<CFQUERY name="EraseRegions"  datasource="XXXXXX" >
				Delete  from Region1
			</cfquery>
			<cfoutput query="qry_getDescription" group="OpUnit">
				<cfif qry_getDescription.JobCode eq 60851 or qry_getDescription.JobCode eq 8074>
						<CFQUERY name="GetRegion"  datasource="XXXXX">
								select * 
								from Region1 r
								where 	r.ManagerEmpID = #EmployeeID#
						</cfquery>
							<cfif GetRegion.recordcount eq 0>
								<!---insert into region Table managerEmpID--->
									<CFQUERY name="qry_insertManger"  datasource="XXXXXX">
										INsert into region1 (name,ManagerEmpID) values ('#qry_getDescription.last_name#',#qry_getDescription.Employeeid#)
									</cfquery>
										<CFQUERY name="GetRegion2"  datasource="xxxxx" dbtype="ODBC">
											select * 
											from Region1 r
											where r.ManagerEmpID = #qry_getDescription.Employeeid#
										</cfquery>
										<cfset holdRegionID = GetRegion2.RegionID>
									<cfquery name="qry_getDescription2" datasource="XXXXX">
										Select * from OrgUnitLocation
										where  jobCode in ('5655','7080') and OpUnit in ('#qry_getDescription.OpUnit#')
										order by OpUnit
								</cfquery>
											<cfloop query="qry_getDescription2">
												<cfif qry_getDescription2.JobCode eq "5655" or qry_getDescription2.JobCode eq "7080">
													<CFQUERY name="qry_insertManger"  datasource="XXXXX">
														Update region1 set SManagerEmpID = #qry_getDescription2.Employeeid# where regionID = #holdRegionID#
													</cfquery>
												</cfif>
											</cfloop>
											<cfquery name="qry_getDescription3" datasource="XXXXXXXX">
												Select * from OrgUnitLocation
												where  jobCode in ('8070','8073')
												order by OpUnit
											</cfquery>
											<!---get all AVP--->
											<cfloop query="qry_getDescription3">
												<cfif qry_getDescription3.JobCode eq "8070" or qry_getDescription3.JobCode eq "8073">
													<CFQUERY name="qry_insertManger"  datasource="XXXXXX">
														Update region1 set AVPEmpID = #qry_getDescription3.Employeeid# where regionID = #holdRegionID#
													</cfquery>
												</cfif>
											</cfloop>
									<!---if there is a record then look for the Sales manager--->
								</cfif>
				</cfif>
											
				</cfoutput>
			Complete

Open in new window

feed.txt
0
 
gdemariaCommented:
Ok, that helped a lot.  Here's the thing I am struggling with...


Here you are adding ONE record to your REGION table...

<CFQUERY name="qry_insertManger"  datasource="XXXXXX">
     INsert into region1 (name,ManagerEmpID) values ('#qry_getDescription.last_name#',#qry_getDescription.Employeeid#)
</cfquery>


But here, you are updating this one record again and again...

<cfloop query="qry_getDescription2">
      <cfif qry_getDescription2.JobCode eq "5655" or qry_getDescription2.JobCode eq "7080">
           <CFQUERY name="qry_insertManger"  datasource="XXXXX">
                Update region1 set SManagerEmpID = #qry_getDescription2.Employeeid#
             where regionID = #holdRegionID#
           </cfquery>
       </cfif>
</cfloop>


Since you are updating the same column and the same record again and again, you are just over-writing the data in every pass of the loop.

Unless you can guaranteed that qry_getDescription2  will only return 1 record you are over writing your data





0
 
erikTsomikSystem Architect, CF programmer Author Commented:
practically it should return only 1 record
0
 
gdemariaCommented:
Ok, we'll see.  

Here is a cleaned version of the code.   You don't need the extra queries and loops, just one update statement.

A couple of observations:

<cfoutput query="qry_getDescription" group="OpUnit">  <---- why the group ?  

You are grouping here by opUnit, but never using the 2nd level of cfoutput to do anything with the group.  You are only using the employeeID from this query... is there only one employeeID per opUnit?  

In your query to populate the AVPEmpID, you were not including the optUNit in your where clause, I added it here because it seemed like an oversight...

    and OpUnit in ('#qry_getDescription.OpUnit#')
<cfsetting requesttimeout="9000">
<cfparam name="holdRegionID" default="0">
 
<cfquery name="qry_getDescription" datasource="xxxxx">
  Select * from OrgUnitLocation
  where  jobCode in ('8074','60851')
  order by OpUnit
</cfquery>
 
<CFQUERY name="EraseAreas"  datasource="XXXXX" >
  Delete  from Area1
</cfquery>
<CFQUERY name="EraseRegions"  datasource="XXXXXX" >
  Delete  from Region1
</cfquery>
 
<cfoutput query="qry_getDescription" group="OpUnit">
 
	<CFQUERY name="GetRegion"  datasource="XXXXX">
	  select * 
	  from Region1 r
	  where   r.ManagerEmpID = #val(qry_getDescription.EmployeeID)#
	</cfquery>
 
    <cfif GetRegion.recordcount eq 0>
         <!---insert into region Table managerEmpID--->
         <CFQUERY name="qry_insertManger"  datasource="XXXXXX">
           insert into region1 (name,ManagerEmpID) values ('#qry_getDescription.last_name#',#qry_getDescription.Employeeid#)
         </cfquery>
         <cfquery name="GetPkey" datasource="#request.datasource#">
          select SCOPE_IDENTITY( ) as ID
         </cfquery>  
         <cfset holdRegionID = GetPkey.ID>
 
		 <CFQUERY name="qry_insertManger"  datasource="XXXXX">
		   Update region1 
		     set SManagerEmpID = (Select Employeeid from OrgUnitLocation
		         where  jobCode in ('5655','7080')
		         and OpUnit in ('#qry_getDescription.OpUnit#')
			     )
		       , AVPEmpID = (Select Employeeid from OrgUnitLocation
		         where  jobCode in ('8070','8073')
		         and OpUnit in ('#qry_getDescription.OpUnit#')
			     )
		  where regionID = #holdRegionID#
		</cfquery>
    </cfif>
                         
</cfoutput>

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
This part is working OK. Now I need to add an Area piece . I was trying to do in the same manner by does not seems to work. First it insert the same regionID. Second, it execute 4 rows and then give the error
<cfsetting requesttimeout="9000">
	<cfquery name="qry_getDescription" datasource="xxxxx">
		Select * from OrgUnitLocation
		where  jobCode in ('8074','60851')
		order by OpUnit
	</cfquery>
	<cfquery name="qry_getArea" datasource="xxxxx">
		Select * from OrgUnitLocation
		where  jobCode in ('7072','8063','8066')
		order by OpUnit
	</cfquery>
	<cfset lOpUnit = ValueList(qry_getDescription.OpUnit)>
	<cfparam name="holdRegionID" default="0">
		<CFQUERY name="EraseAreas"  datasource="xxxxx" dbtype="ODBC">
				Delete  from Area1
			</cfquery>
			<CFQUERY name="EraseRegions"  datasource="xxxxx" dbtype="ODBC">
				Delete  from Region1
			</cfquery>
			<cfoutput query="qry_getDescription" group="OpUnit">
						<CFQUERY name="GetRegion"  datasource="xxxxx">
								select * 
								from Region1 r
								where 	r.ManagerEmpID = #EmployeeID#
						</cfquery>
							<cfif GetRegion.recordcount eq 0>
								<!---insert into region Table managerEmpID--->
									<CFQUERY name="qry_insertManger"  datasource="xxxxx">
										INsert into region1 (name,ManagerEmpID,code) 
										values 
										('#qry_getDescription.last_name#',#qry_getDescription.Employeeid#,'#left(qry_getDescription.Employeeid,5)#')
									</cfquery>
										<cfquery name="GetPkey" datasource="xxxxx">
         							 select SCOPE_IDENTITY( ) as ID
        					 </cfquery>  
         						<cfset holdRegionID = GetPkey.ID>
									
											<!----**************Area spot ************--->
											<cfloop query="qry_getArea">
												<CFQUERY name="GetArea"  datasource="xxxxx">
													select * 
													from Area1 a
													where 	a.ManagerEmpID = #EmployeeID#
												</cfquery>
												<cfif GetArea.recordcount eq 0>
														<CFQUERY name="qry_insertManger4"  datasource="xxxxx">
															insert into Area1 (name,ManagerEmpID,regionID)
															values 
															(
																'#qry_getArea.Last_Name#',#qry_getArea.Employeeid#,#holdRegionID#
																
															)
														</cfquery>
												</cfif>
												<cfquery name="GetPkey2" datasource="xxxxx">
         							 		select SCOPE_IDENTITY( ) as ID
        							 </cfquery>  
         								<cfset holdAreaID = GetPkey2.ID>
												<CFQUERY name="qry_insertManger"  datasource="xxxxx">
                  			 	Update Area1 
                     		 	set SManagerEmpID = (Select top 1 Employeeid from OrgUnitLocation
                         	where  jobCode in ('7073') and OpUnit in ('#qry_getArea.OpUnit#'))
                       		, ADRCEmpID = (Select top 1 Employeeid from OrgUnitLocation
                        	 where  jobCode in ('1130') and OpUnit in ('#qry_getArea.OpUnit#'))
			                  	where AreaID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdAreaID#">
               				 </cfquery>
												
											</cfloop>
											
											<!---**************End of Area SPot **************--->
										
										<!---get all SManagerID--->
											  <CFQUERY name="qry_insertManger"  datasource="xxxxx">
                  			 Update region1 
                     		 set SManagerEmpID = (Select Employeeid from OrgUnitLocation
                         where  jobCode in ('5655','7080') and OpUnit in ('#qry_getDescription.OpUnit#'))
                       , AVPEmpID = (Select Employeeid from OrgUnitLocation
                         where  jobCode in ('8070','8073') and OpUnit in ('#qry_getDescription.OpUnit#'))
			                  where regionID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdRegionID#">
                </cfquery>
									<!---if there is a record then look for the Sales manager--->
								</cfif>
			
											
				</cfoutput>
			Complete feed congratulations

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
how would i add the piece that would update the Location piece(commented out part)
<cfsetting requesttimeout="9000">
	<cfquery name="qry_getDescription" datasource="XXXXX">
		Select * from OrgUnitLocation
		where  Position = 'AVP' and PrimaryPosition = 1
		order by OpUnit
	</cfquery>
	<cfquery name="qry_getArea" datasource="XXXXX">
		Select * from OrgUnitLocation
		where  Position = 'AM' and PrimaryPosition = 1
		order by OpUnit
	</cfquery>
	
	
	<cfset lLocationID = ValueList(qry_getArea.OpUnit)>
	<cfparam name="holdRegionID" default="0">
	<cfparam name="holdAreaID" default="0">
			<CFQUERY name="eraseLocArea"  datasource="XXXXX">
				Update Location
					Set AreaID = NULL
			</cfquery>
			<CFQUERY name="EraseAreas"  datasource="XXXXX">
				Delete  from Area
			</cfquery>
			<CFQUERY name="EraseRegions"  datasource="XXXXX">
				Delete  from Region
			</cfquery>
		
		
			<cfoutput query="qry_getDescription" group="OpUnit">
						<CFQUERY name="GetRegion"  datasource="XXXXX">
								select * 
								from Region r
								where 	r.ManagerEmpID = #EmployeeID#
						</cfquery>
							<cfif GetRegion.recordcount eq 0>
								<!---insert into region Table managerEmpID--->
									<CFQUERY name="qry_insertManger"  datasource="XXXXX">
										INsert into region (name,ManagerEmpID,code) 
										values 
										('#qry_getDescription.last_name#',#qry_getDescription.Employeeid#,'#left(qry_getDescription.Employeeid,5)#')
									</cfquery>
										<cfquery name="GetPkey" datasource="XXXXX">
         							 select SCOPE_IDENTITY( ) as ID
        					 </cfquery>  
         						<cfset holdRegionID = GetPkey.ID>
									
											<!----**************Area spot ************--->
											<cfloop query="qry_getArea">
												<CFQUERY name="GetArea"  datasource="XXXXX">
													select * 
													from Area a
													where 	a.ManagerEmpID = #EmployeeID#
												</cfquery>
												<cfif GetArea.recordcount eq 0>
														<CFQUERY name="qry_insertManger4"  datasource="XXXXX">
															insert into Area (name,ManagerEmpID,regionID)
															values 
															(
																'#qry_getArea.Last_Name#',#qry_getArea.Employeeid#,#holdRegionID#
																
															)
														</cfquery>
												</cfif>
												<cfquery name="GetPkey2" datasource="XXXXX">
         							 		select SCOPE_IDENTITY( ) as ID
        							 </cfquery>  
         								<cfset holdAreaID = GetPkey2.ID>
											
												
												
												<CFQUERY name="qry_insertManger"  datasource="XXXXX">
                  			 	Update Area 
                     		 	set SManagerEmpID = (Select top 1 Employeeid from OrgUnitLocation
                         	where  Position = 'ASM' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
                       		, ADRCEmpID = (Select top 1 Employeeid from OrgUnitLocation
                        	 where  Position = 'ADRC' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
			                  	where AreaID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdAreaID#">
               				 </cfquery>
												
					
												
											</cfloop>
											
											<!---**************End of Area SPot **************--->
										
										<!---get all SManagerID--->
											  <CFQUERY name="qry_insertManger"  datasource="XXXXX">
                  			 Update region1 
												 	<cfif qry_getDescription.Position eq 'VPFSM' and qry_getDescription.PrimaryPosition eq 1>
                     		 set SManagerEmpID = (Select Employeeid from OrgUnitLocation
                         where  Position IN ('VPFSM') and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getDescription.OpUnit)#'))
												 <cfelse>
												 	set SManagerEmpID = (Select Employeeid from OrgUnitLocation
                         where  Position IN ('AVPFSM') and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getDescription.OpUnit)#'))
												 </cfif>
                       , AVPEmpID = (Select Employeeid from OrgUnitLocation
                         where  Position = 'SAM' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getDescription.OpUnit)#'))
			                  where regionID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdRegionID#">
                </cfquery>
									<!---if there is a record then look for the Sales manager--->
								<!---	<CFQUERY name="qry_UpdateLocation"  datasource="XXXXX">
													Update Location set AreaID = #GetPkey2.ID# where OpUnit  = #qry_getDescription.OpUnit#
													
												</cfquery>
												The OpUnit &nbsp;#qry_getDescription.OpUnit# was Updated to #GetPkey2.ID#--->
								</cfif>
			
											
				</cfoutput>
			Complete feed congratulations

Open in new window

0
 
gdemariaCommented:
Erik, is this the question you were writing to me about?   Is your last post the latest code update?

You are trying to figure out how to update the location as in..

   Update Location set AreaID = #GetPkey2.ID# where OpUnit  = #qry_getDescription.OpUnit#

What is your question about this?  Where is the difficulty?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
when i update the regionID in the area table it put all the same regionIDs. here is my latest code. I seems to got it to work but would you mind take a look at
<cfsetting requesttimeout="9000">
<cfparam name="holdRegionID" default="0">
	<cfquery name="qry_getDescription" datasource="XXXXX">
		Select * from OrgUnitLocation
		where  Position = 'AVP' and PrimaryPosition = 1
		order by OpUnit
	</cfquery>
	
	
	
<!---	<cfset lLocationID = ValueList(qry_getArea.OpUnit)>--->
	<cfparam name="holdRegionID" default="0">
	<cfparam name="holdAreaID" default="0">
			<CFQUERY name="eraseLocArea"  datasource="XXXXX">
				Update Location
					Set AreaID = NULL
			</cfquery>
			<CFQUERY name="EraseAreas"  datasource="XXXXX">
				Delete  from Area
			</cfquery>
			<CFQUERY name="EraseRegions"  datasource="XXXXX">
				Delete  from Region
			</cfquery>
		
		
			<cfoutput query="qry_getDescription" group="OpUnit">
						<CFQUERY name="GetRegion"  datasource="XXXXX">
								select * 
								from Region r
								where 	r.ManagerEmpID = #val(qry_getDescription.EmployeeID)#
						</cfquery>
							<cfif GetRegion.recordcount eq 0>
								
								<!---insert into region Table managerEmpID--->
									<CFQUERY name="qry_insertManger"  datasource="XXXXX">
										INsert into region (name,ManagerEmpID,code) 
										values 
										('#qry_getDescription.last_name#',#qry_getDescription.Employeeid#,'#left(qry_getDescription.Employeeid,5)#')
									</cfquery>
										<cfquery name="GetPkey" datasource="XXXXX">
         							 select SCOPE_IDENTITY( ) as ID
        					 </cfquery>  
         						<cfset holdRegionID = GetPkey.ID>
											<!---get all SManagerID--->
											  <CFQUERY name="qry_insertManger"  datasource="XXXXX">
                  			 Update region 
												 	<cfif qry_getDescription.Position eq 'VPFSM' and qry_getDescription.PrimaryPosition eq 1>
                     		 set SManagerEmpID = (Select Employeeid from OrgUnitLocation
                         where  Position IN ('VPFSM') and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getDescription.OpUnit)#'))
												 <cfelse>
												 	set SManagerEmpID = (Select Employeeid from OrgUnitLocation
                         where  Position IN ('AVPFSM') and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getDescription.OpUnit)#'))
												 </cfif>
                       , AVPEmpID = (Select Employeeid from OrgUnitLocation
                         where  Position = 'SAM' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getDescription.OpUnit)#'))
			                  where regionID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdRegionID#">
                </cfquery>
										</cfif>
											<!----**************Area spot ************--->
										</cfoutput>
												<cfquery name="qry_getArea" datasource="XXXXX">
													Select * from OrgUnitLocation
													where  Position = 'AM' and PrimaryPosition = 1 <!---and OpUnit =  #qry_getDescription.OpUnit#--->
													order by OpUnit
											</cfquery>
											<cfloop query="qry_getArea">
												<CFQUERY name="GetArea"  datasource="XXXXX">
													select * 
													from Area a
													where 	a.ManagerEmpID = #qry_getArea.EmployeeID#
												</cfquery>
												<cfif GetArea.recordcount eq 0>
														<CFQUERY name="qry_insertManger4"  datasource="XXXXX">
															insert into Area (name,ManagerEmpID)
															values 
															(
																'#qry_getArea.Last_Name#',#qry_getArea.Employeeid#
																
															)
														</cfquery>
														<cfquery name="GetPkey2" datasource="XXXXX">
         							 		select SCOPE_IDENTITY( ) as ID
        							 </cfquery>  
         								<cfset holdAreaID = GetPkey2.ID>
												
												</cfif>
											<CFQUERY name="qry_insertManger5"  datasource="XXXXX">
                  			 	Update Area 
                     		 	set SManagerEmpID = (Select top 1 Employeeid from OrgUnitLocation
                         	where  Position = 'ASM' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
                       		, ADRCEmpID = (Select top 1 Employeeid from OrgUnitLocation
                        	 where  Position = 'ADRC' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#')),
													 regionID = (Select  r.regionID from OrgUnitLocation
													 inner join Region r on r.ManagerEmpID = OrgUnitLocation.EmployeeID
                         	where  OrgUnitLocation.Position = 'AVP' and OrgUnitLocation.PrimaryPosition = 1 
													and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
			                  	where AreaID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdAreaID#">
               				 </cfquery>
												<cfquery name="getLocations" datasource="XXXXX">
													Select opUnit from Location where OpUnit = '#trim(qry_getArea.OpUnit)#'
													order by OpUnit
												</cfquery>		
												<cfif getLocations.RecordCount gt 0>
													<cfquery name="qry_UpdateLocation" datasource="XXXXX">
														Update Location set AreaID = #holdAreaID# where opUnit = '#trim(qry_getArea.OpUnit)#'
													</cfquery>
												</cfif>
												
										
												</cfloop>
				
			Complete feed congratulations

Open in new window

0
 
gdemariaCommented:
Are you referring to this update statement ?

Update Area
               set SManagerEmpID = (Select top 1 Employeeid from OrgUnitLocation
                                    where  Position = 'ASM' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
                 , ADRCEmpID = (Select top 1 Employeeid from OrgUnitLocation
                                where  Position = 'ADRC' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
                   , regionID = (Select  r.regionID
                          from OrgUnitLocation
                        inner join Region r on r.ManagerEmpID = OrgUnitLocation.EmployeeID
                        where  OrgUnitLocation.Position = 'AVP' and OrgUnitLocation.PrimaryPosition = 1
                        and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
            where AreaID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdAreaID#">


Here are some observations, not sure if any will help...
<cfloop query="qry_getArea">
		<CFQUERY name="GetArea" datasource="XXXXX">
			select * 
			from Area a
			where 	a.ManagerEmpID = #qry_getArea.EmployeeID#
		</cfquery>
		<cfif GetArea.recordcount eq 0>
			<CFQUERY name="qry_insertManger4" datasource="XXXXX">
				insert into Area (name,ManagerEmpID)
				values ('#qry_getArea.Last_Name#',#qry_getArea.Employeeid#)
			</cfquery>
			<cfquery name="GetPkey2" datasource="XXXXX">
				select SCOPE_IDENTITY( ) as ID
			</cfquery>
		    <cfset holdAreaID = GetPkey2.ID>
 
		</cfif>
 
**** If a record already exists in GetArea then HoldAreaID is either 0 or the value from the previous loop, it is not set.
		
		<CFQUERY name="qry_insertManger5" datasource="XXXXX">
		Update Area 
		   set SManagerEmpID = (Select top 1 Employeeid from OrgUnitLocation
		                        where  Position = 'ASM' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
		     , ADRCEmpID = (Select top 1 Employeeid from OrgUnitLocation
		                    where  Position = 'ADRC' and PrimaryPosition = 1 and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
  		     , regionID = (Select  r.regionID 
			        from OrgUnitLocation
            		inner join Region r on r.ManagerEmpID = OrgUnitLocation.EmployeeID
		            where  OrgUnitLocation.Position = 'AVP' and OrgUnitLocation.PrimaryPosition = 1 
		            and OpUnit in ('#trim(qry_getArea.OpUnit)#'))
		where AreaID = <cfqueryparam cfsqltype="cf_sql_integer" value="#holdAreaID#">
		</cfquery>
 
 
****** Can you show what the value of #trim(qry_getArea.OpUnit)#  may be?
this seems to be the wrong syntax:  OpUnit in ('#trim(qry_getArea.OpUnit)#') 
 
Is it a string or number?  
 
Should it be a string: OpUnit = '#trim(qry_getArea.OpUnit)#'
 
or a number?    OpUnit = #val(qry_getArea.OpUnit)#

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
it is a string
0
 
gdemariaCommented:
Ok, so get rid of the in () clause.

what about HoldAreaID  is it ok that you don't set that if the record is not found?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
it is ok
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
now the problem is the following. Some locations do not get areaID. So the same AreaManager can be in multiple locations
0
 
gdemariaCommented:

 Ok.....  so what's the problem?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
OK. Now my requirements has changed.
this query
      Select * from OrgUnitLocation
            where  Position = 'AVP' and PrimaryPosition = 1
            order by OpUnit

Must be changed to this
Select * from OrgUnitLocation
            where  Position in( 'AVP','VPFSM','AVPFSM','AM','ASM','ADRC','SAM') and PrimaryPosition = 1
            order by OpUnit

So will loop through the community
And then the region will be defined by combination of 3 not just by 1 AVP
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now