Skip to main content
Inspiring
November 20, 2009
Question

Nested query doesn't return result

  • November 20, 2009
  • 1 reply
  • 718 views

Using CF 8.1.  First field in table is "item" which is a compound filed containing a five digit customer number, a space, then a six digit item number.  I need to produce a list of items for a specific customer based on their customer number, which is stored in a session variable. That part of my code all works. However, inside the output I need to retreive the item pack size from another table. This part of my code isn't working (QryAddInfo). Code pasted below. I'm wondering if it's because I'm trying to have a third query or that it is inside an output???

          <cfquery name="QryGetInfo" datasource="necsodbc">
          SELECT custnoitem, descrip, descrip4, lastdate
          FROM arcpric
          ORDER BY custnoitem
          </cfquery>

  

         <cfquery name="GetItems" dbtype="query">
          SELECT *
          FROM QryGetInfo
          WHERE #left(QryGetInfo.custnoitem, 5)# = #session.MemberID#
          ORDER BY custnoitem
          </cfquery>

<body>
<div align="center">
<br /><br />
Standard Order <cfoutput>#session.MemberID#</cfoutput>
<br /><br />
<table cellpadding="2">
<tr>
<td>ITEM</td>
<td>DESCRIPTION</td>
<td>PACK</td>
<td>ITEM<br /> COMMITMENT</td>
<td>QTY</td>
<td>LAST<BR />ORDERED</td>
</tr>
<cfoutput query="QryGetInfo">
<CFIF #left(QryGetInfo.custnoitem, 5)# eq #session.MemberID# AND #MID(QryGetInfo.custnoitem, 7,6)# LT 900000>
<TR height="22">
<TD>#MID(QryGetInfo.custnoitem, 7,6)#</TD>
<TD align="left">#QryGetInfo.DESCRIP#</TD>

<td>
<cfquery name="GetAddInfo" datasource="necsodbc">
SELECT SIZE
FROM ARINVT01
WHERE ITEM = '#MID(QryGetInfo.custnoitem, 7,6)#'
</cfquery>
#GetAddInfo.size#
</td>

<TD align="left">
<div align="center">
<cfif ASC(left(QryGetInfo.DESCRIP4,2)) NEQ 32>
YES
</cfif>
</div>
</TD>
<TD width="25"> </TD>
<TD>#QryGetInfo.LASTDATE#</TD>
</TR>
</CFIF>
</cfoutput>
</table>
</div>
</body>

This topic has been closed for replies.

1 reply

Inspiring
November 20, 2009

Your approach of running database queries inside a loop is very inefficient and shouldn't be necessary.  A better approach would be to run a single query that gets the data from both tables.  If you don't know how to do that, I've heard good things about the book, Teach Yourself SQL in 10 Minutes by Ben Forta.

ghouserAuthor
Inspiring
November 20, 2009

The reason I didn't do it the "proper" way is because "size" can only be retreived once the item number is determined. I know what I have is inefficient, and that it is best to get all data with a single query, but without the item number being determined until after the second query it seemed this was not possible.

Inspiring
November 20, 2009

this logic

WHERE ITEM = '#MID(QryGetInfo.custnoitem, 7,6)#'

in a single query would be

select fields

from table1 join table 2 on item = substr(custnoitem, 7, 6)