CFQUERY - Calling results from one to supply inputs foranother

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default CFQUERY - Calling results from one to supply inputs foranother

    Here's my code...

    ------------------------------------

    <cfquery name="Recordset1" datasource="DB1">
    SELECT *
    FROM dbo.tbl_Main
    LEFT JOIN dbo.tbl_Order
    ON dbo.tbl_Main.Reference_Code = dbo.tbl_Order.Reference_Code
    WHERE dbo.tbl_Main.Prep_Date <> '1/1/1900' AND
    dbo.tbl_Main.Fleet_Delivery_Date IN ('1/1/1900',' ')
    ORDER BY dbo.tbl_Main.Stock_Number
    ASC
    </cfquery>

    <cfquery name="Recordset2" datasource="DB2">
    SELECT * FROM dbo.Vehicle WHERE StockNo = '#Recordset1.Stock_Number#'
    </cfquery>

    <table width="600" border="0" cellspacing="1" cellpadding="4">
    <tr>
    <td width="75"><u><strong><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">Stock
    #</font></strong></u></td>
    <td width="75"><u><strong><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">Serial
    #</font></strong></u></td>
    <td width="75"><u><strong><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">GSO</font></strong></u></td>
    <td width="75"><u><strong><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">Model</font></strong></u></td>
    <td width="100"><u><strong><font size="2" face="Tahoma, Verdana,
    Helvetica, sans-serif">Received</font></strong></u></td>
    <td width="100"><u><strong><font size="2" face="Tahoma, Verdana,
    Helvetica, sans-serif">Prepped</font></strong></u></td>
    <td width="100"><u><strong><font size="2" face="Tahoma, Verdana,
    Helvetica, sans-serif">Picked
    Up</font></strong></u></td>
    </tr>

    <cfloop query="Recordset1"> <cfoutput>
    <tr>
    <td><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">#Recordset1.Stock_Number#</font></td>
    <td><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">#Recordset2.VehID#</font></td>
    <td><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">#Recordset1.GSO_Number#</font></td>
    <td><font size="2" face="Tahoma, Verdana, Helvetica,
    sans-serif">#Recordset2.Model#</font></td>
    <td>
    <font size="2" face="Tahoma, Verdana, Helvetica, sans-serif">
    <cfif #Recordset1.NTG_Delivery_Date# EQ "01/01/1900">
    - - - -
    <cfelse>
    #LSDateFormat(Recordset1.NTG_Delivery_Date,'M/DD/YY')#</font>
    </cfif>
    </td>
    <td>
    <font size="2" face="Tahoma, Verdana, Helvetica, sans-serif">
    <cfif #Recordset1.Prep_Date# EQ "01/01/1900">
    - - - -
    <cfelse>
    #LSDateFormat(Recordset1.Prep_Date,'M/DD/YY')#</font>
    </cfif>
    </td>
    <td>
    <font size="2" face="Tahoma, Verdana, Helvetica, sans-serif">
    <cfif #Recordset1.Fleet_Delivery_Date# EQ "01/01/1900">
    - - - -
    <cfelse>
    #LSDateFormat(Recordset1.Fleet_Delivery_Date,'M/DD/YY')#</font>
    </cfif>
    </td> </tr>
    </cfoutput>
    </cfloop>


    -----------------------------

    I am trying to pull information from two databases via two separate, yet
    incorporated cfquery.
    Yet the output gives me the same repeated field value for

    Recordset2.Model and Recordset2.VehID

    I want those values to change based on which Stock_Number is running in the
    first query.

    IT OUTPUTS LIKE THIS:

    Stock # Serial # GSO Model Received Prepped Picked Up
    7340-N 6M026642 20375 MR688S 4/19/05 4/26/05 - - - -
    7520-N 6M026642 MR688S 3/23/05 3/24/05 - - - -
    7589-N 6M026642 20371 MR688S 4/21/05 4/29/05 - - - -
    7698-N 6M026642 MR688S 4/01/05 4/11/05 - - - -
    7877-N 6M026642 22291 MR688S 3/07/05 3/08/05 - - - -
    8223-N 6M026642 MR688S 4/05/05 4/11/05 - - - -

    I would like the serial # and models to change with the changing stock #s.



    jvanlaere Guest

  2. Similar Questions and Discussions

    1. #31618 [Com]: is_readable() results based on ownership of calling script, not file
      ID: 31618 Comment by: nobody at bugs dot php dot net Reported By: kpederson at mail dot ewu dot edu Status: ...
    2. CFQuery displaying old results?
      No matter what i try, my cfquery is displaying old results. I've tried inserting cachedwithin="#createtimespan(0,0,0,0)#" into the cfquery, but it...
    3. Results of update in CFQUERY
      And what about delete? Seems like delete does not return anything neither.
    4. Calling Multiple Stored Procedures inside a cfquery
      All, I have ColdFusion page that contains a Company dropdown, a To Date and From Date and submit button The user can select an ?individual...
    5. Help! Having trouble calling codebehind function from repeater results!
      Hi, I have a data repeater that is returning a list of events. One of the things I want to do is give the user a way to edit or delete an event....
  3. #2

    Default Re: CFQUERY - Calling results from one to supply inputsfor another

    Your main problem is that you are looping through recordset1, but only using
    the first record of recordset2

    First does the first query give you the correct results ?

    I'm not sure I understand the "AND" clause. I would only ever have 2 values
    in a date column, a valid date or NULL
    I would also pass the data as an ODBC date type, so the clauses would be

    WHERE dbo.tbl_Main.Prep_Date <> #CreateODBCDate(1/1/1900)#
    AND (dbo.tbl_Main.Fleet_Delivery_Date = #CreateODBCDate(1/1/1900)# OR
    dbo.tbl_Main.Fleet_Delivery_Date Is Null)


    Next, Do you need 2 queries or can they be combined ? If you can combine
    them, then

    SELECT *
    FROM dbo.tbl_Main
    LEFT JOIN dbo.tbl_Order
    ON dbo.tbl_Main.Reference_Code = dbo.tbl_Order.Reference_Code
    INNER JOIN dbo.Vehicle ON dbo.Vehicle.StockNo = dbo.tbl_Main.Stock_Number
    WHERE dbo.tbl_Main.Prep_Date <> #CreateODBCDate("1/1/1900")#
    AND (dbo.tbl_Main.Fleet_Delivery_Date = #CreateODBCDate("1/1/1900")# OR
    dbo.tbl_Main.Fleet_Delivery_Date Is Null)
    AND dbo.Vehicle.StockNo = '#Recordset1.Stock_Number#'

    I would also remove the "*" from the select list and list the actual columns
    you need.

    Depending upon the returned result set, you may need to change the INNER JOIN
    to an OUTER JOIN to get the correct results.

    Ken


    The ScareCrow Guest

  4. #3

    Default Re: CFQUERY - Calling results from one to supply inputsfor another

    Thanks for the response, Ken.
    The first result is correct, so that is indicating a correct pull.

    I do not think I can combine the queries because they from two different datasources.
    jvanlaere Guest

  5. #4

    Default Re: CFQUERY - Calling results from one to supply inputsfor another

    I figured it out...

    <cfquery name="Recordset1" datasource="DB1">
    SELECT *
    FROM dbo.tbl_Main
    LEFT JOIN dbo.tbl_Order
    ON dbo.tbl_Main.Reference_Code = dbo.tbl_Order.Reference_Code
    WHERE dbo.tbl_Main.Prep_Date <> #CreateODBCDate("1/1/1900")#
    AND (dbo.tbl_Main.Fleet_Delivery_Date = #CreateODBCDate("1/1/1900")# OR
    dbo.tbl_Main.Fleet_Delivery_Date Is Null)
    ORDER BY dbo.tbl_Main.Stock_Number
    ASC
    </cfquery>

    <cfquery name="Recordset2" datasource="DB2">
    SELECT * FROM dbo.Vehicle
    </cfquery>

    <!--- Combine the results --->
    <CFQUERY NAME="combined" DBTYPE="query">
    SELECT *
    FROM Recordset1, Recordset2
    WHERE Recordset2.StockNo = Recordset1.Stock_Number
    </CFQUERY>

    Thanks!

    jvanlaere Guest

  6. #5

    Default Re: CFQUERY - Calling results from one to supply inputsfor another

    Because of the syntax your using, I'm assuming the db is sql server. If this
    is the case, then you can create a stored procedure to return the required
    recordset. This would then enable you to combine the queries into one query
    and only have to have the single db call in cf.

    Ken

    The ScareCrow Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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