Compile function Copy_RUN_STAGE_Record_To_SQL(Connection, Key, Record)

/*****************************************************************************\
	Copies the given RUN_STAGE_WFR record to the MSSQL database.
	
	History
	-------
	11/29/2016    KRF   Original Programmer
\*****************************************************************************/

Declare function SQL_Write, SQL_Write_MV, SQL_Format
Ans = ""

$insert RUN_STAGE_EQUATES

// Parse record into a dimensioned array for speed
Dim Rec(13)
MatParse Record into Rec
					    
// List of key names and their values
Keys  = "RDS_NO":@VM:SQL_Format(Field(Key, "*", 1), "INT"):@FM
Keys := "STAGE" :@VM:SQL_Format(Field(Key, "*", 2), "STR")

// List of data fields and their values
DataFields  = "WFR_CNT"               :@VM:SQL_Format(Rec(RUN_STAGE_WFR_CNT$),        "INT"):@FM
DataFields := "TOOL_ID"               :@VM:SQL_Format(Rec(RUN_STAGE_TOOL_ID$),        "STR"):@FM
DataFields := "COMMENT"               :@VM:SQL_Format(Rec(RUN_STAGE_COMMENT$),        "STR"):@FM
DataFields := "COMP_BY"               :@VM:SQL_Format(Rec(RUN_STAGE_COMP_BY$),        "STR"):@FM
DataFields := "COMP_DTM"              :@VM:SQL_Format(Rec(RUN_STAGE_COMP_DTM$),       "DATETIME"):@FM
DataFields := "STAGE_BYPASSED"        :@VM:SQL_Format(Rec(RUN_STAGE_STAGE_BYPASSED$), "BIT"):@FM
DataFields := "ENTER_DTM"             :@VM:SQL_Format(Rec(RUN_STAGE_ENTER_DTM$),      "STR"):@FM

// Symbolics
DataFields := "DEST_TEST"             :@VM:SQL_Format({DEST_TEST},  "BIT"):@FM
DataFields := "GAN_STAGE"             :@VM:SQL_Format({GAN_STAGE},  "STR"):@FM
DataFields := "REACT_TYPE"            :@VM:SQL_Format({REACT_TYPE}, "STR"):@FM
DataFields := "WO_NO"                 :@VM:SQL_Format({WO_NO},      "STR")

// Write the data to the SQL database
Ans = SQL_Write(Connection, "RUN_STAGE", Keys, DataFields)

//-------------------------------------------------------------------------------------------------
// Multi-valued Fields

// RUN_STAGE_INV_ACTIONS
If Ans EQ "" AND Rec(RUN_STAGE_SPEC_INV_ACTION$) NE "" then
        
    // List of data fields and their values
    MvFields  = "SPEC_INV_ACTION" :@FM:SQL_Format(Rec(RUN_STAGE_SPEC_INV_ACTION$), "STR"):@RM
    MvFields := "SIA_COMP_BY"     :@FM:SQL_Format(Rec(RUN_STAGE_SIA_COMP_BY$),     "STR"):@RM
    MvFields := "SIA_COMP_DTM"    :@FM:SQL_Format(Rec(RUN_STAGE_SIA_COMP_DTM$),    "DATETIME")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "RUN_STAGE_INV_ACTIONS", Keys, MvFields)
    
end

// RUN_STAGE_SUSC_LOAD
Pocket = {POCKET}
If Ans EQ "" AND Pocket NE "" then
        
    // List of data fields and their values
    MvFields  = "POCKET"        :@FM:SQL_Format(Pocket,         "INT"):@RM
    MvFields := "POCKET_CHAR"   :@FM:SQL_Format({POCKET_CHAR},  "STR"):@RM
    MvFields := "ZONE"          :@FM:SQL_Format({ZONE},         "STR"):@RM
    MvFields := "PKT_SAT_ID"    :@FM:SQL_Format({PKT_SAT_ID},   "STR"):@RM
    MvFields := "WAFER_CHAR"    :@FM:SQL_Format({WAFER_CHAR},   "STR"):@RM
    MvFields := "IN_SLOT_ID"    :@FM:SQL_Format({IN_SLOT_ID},   "STR"):@RM
    MvFields := "IN_WFR_ID"     :@FM:SQL_Format({IN_WFR_ID},    "STR"):@RM
    MvFields := "CARR_SLOT_ID"  :@FM:SQL_Format({CARR_SLOT_ID}, "STR"):@RM
    MvFields := "OUT_SLOT_ID"   :@FM:SQL_Format({OUT_SLOT_ID},  "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "RUN_STAGE_SUSC_LOAD", Keys, MvFields)
    
end

// RUN_STAGE_CARRIER
CarrSlotNo = {CARR_SLOT_NO}
If Ans EQ "" AND Pocket NE "" then
        
    // List of data fields and their values
    MvFields  = "CARR_SLOT_NO"  :@FM:SQL_Format(CarrSlotNo,     "STR"):@RM
    MvFields := "CARR_WFR_ID"   :@FM:SQL_Format({CARR_WFR_ID},  "STR"):@RM
    MvFields := "CARR_SCRIBE"   :@FM:SQL_Format({CARR_SCRIBE},  "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "RUN_STAGE_CARRIER", Keys, MvFields)
    
end

Return Ans
