Search code examples
asp-classic

Multiple Tables Linked By ID


I have to write an ASP page that has connection to 1 database and then queries two tables one which has the header detail in and then the second which has the order lines in, each table has a ORDER_NUMNER.

These Tables contain a sales orders which I need to print out into an HTML page any help on this would be great as ASP is not my main language.


Solution

  • In general:

    Instantiate and open your database connection: (see www.connectionstrings.com for more information)

    dim conn
    conn.open "your connection string goes here"
    

    Open a recordset for the master table and detail table:

    dim rst
    rst.open "select * from tblMaster left join tblDetail on tblMaster.ORDER_NUMBER = tblDetail.ORDER_NUMBER where ORDER_NUMBER = 4",conn,1,3
    

    Exit out of there are not records

    if rst.eof then
       rst.close
       conn.close
       Response.end
    end if
    

    Print header info (for fields order_date, order_number, and order_company:

    response.write "Company: " & rst.fields("order_company") & "<br>"
    response.write "Date: " & rst.fields("order_date") & "<br>"
    response.write "Order Number: " & rst.fields("order_number") & "<br>"
    

    Loop through records, reading all records from detail table: (for fields item_desc, item_qty, item_cost)

    while not rst.eof
        response.write "Item: " & rst.fields("item_desc") & "<br>"
        response.write "Qty: " & rst.fields("item_qty") & "<br>"
        response.write "Cost: " & rst.fields("item_cost") & "<br>"
        rst.MoveNext
    wend
    

    Close the recordset

    rst.close
    

    Close the connection

    conn.close