Solved

Cfquery and array problem

Posted on 2009-05-13
21
633 Views
Last Modified: 2013-12-24
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
Comment
Question by:erikTsomik
  • 10
  • 10
21 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 24380106
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 24381451
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 24381454
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24381726
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 24385171
> 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
 
LVL 39

Expert Comment

by:gdemaria
ID: 24386708
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24386908
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 24396271
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24396818
practically it should return only 1 record
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24396976
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 19

Author Comment

by:erikTsomik
ID: 24424140
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24541320
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 24549063
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24549123
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
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 24549416
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24549444
it is a string
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24549462
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 24549726
it is ok
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 24549908
now the problem is the following. Some locations do not get areaID. So the same AreaManager can be in multiple locations
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24550190

 Ok.....  so what's the problem?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 24550551
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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now