Compile function Copy_RUN_STAGE_WFR_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_WFR_EQUATES

// Parse record into a dimensioned array for speed
Dim Rec(9)
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"):@FM
Keys := "WFR_ID":@VM:SQL_Format(Field(Key, "*", 3), "STR")

// List of data fields and their values
DataFields  = "SPEC_TOOL_CLASS"       :@VM:SQL_Format(Rec(RUN_STAGE_WFR_SPEC_TOOL_CLASS$), "STR"):@FM
DataFields := "TOOL_ID"               :@VM:SQL_Format(Rec(RUN_STAGE_WFR_TOOL_ID$),         "STR"):@FM
DataFields := "COMMENT"               :@VM:SQL_Format(Rec(RUN_STAGE_WFR_COMMENT$),         "STR"):@FM
DataFields := "COMP_BY"               :@VM:SQL_Format(Rec(RUN_STAGE_WFR_COMP_BY$),         "STR"):@FM
DataFields := "COMP_DTM"              :@VM:SQL_Format(Rec(RUN_STAGE_WFR_COMP_DTM$),        "DATETIME"):@FM

// Symbolics
DataFields := "COMP_DT"               :@VM:SQL_Format({COMP_DT},       "DATE"):@FM
DataFields := "CURR_STATUS"           :@VM:SQL_Format({CURR_STATUS},   "STR"):@FM
DataFields := "DEST_TEST"             :@VM:SQL_Format({DEST_TEST},     "BIT"):@FM
DataFields := "EPI_PART_NO"           :@VM:SQL_Format({EPI_PART_NO},   "STR"):@FM
DataFields := "GAN_RUN_ID"            :@VM:SQL_Format({GAN_RUN_ID},    "STR"):@FM
DataFields := "GAN_WFR_STAGE"         :@VM:SQL_Format({GAN_WFR_STAGE}, "STR"):@FM
DataFields := "IA_COMP_BY"            :@VM:SQL_Format({IA_COMP_BY},    "STR"):@FM
DataFields := "INV_ACTIONS"           :@VM:SQL_Format({INV_ACTIONS},   "STR"):@FM
DataFields := "SCRIBE"                :@VM:SQL_Format({SCRIBE},        "STR"):@FM
DataFields := "START_DTM"             :@VM:SQL_Format({START_DTM},     "DATETIME"):@FM
DataFields := "STOP_DTM"              :@VM:SQL_Format({STOP_DTM},      "DATETIME"):@FM
DataFields := "TIME_ON_TOOL"          :@VM:SQL_Format({TIME_ON_TOOL},  "DEC", 2):@FM
DataFields := "WO_NO"                 :@VM:SQL_Format({WO_NO},         "STR")

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

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

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

// RUN_STAGE_WFR_QIN_WFR_ID
If Ans EQ "" and {QIN_WFR_ID} NE "" then
    
    // List if data fields and their values
    MvFields = "QIN_WFR_ID" :@FM:SQL_Format({QIN_WFR_ID}, "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "RUN_STAGE_WFR_QIN_WFR_ID", Keys, MvFields)
end

Return Ans
