Compile function Copy_PM_SPEC_Record_To_SQL(Connection, Key, Record)

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

Declare function SQL_Write, SQL_Write_MV, SQL_Format
Ans = ""

$insert PM_SPEC_EQUATES

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

// List of data fields and their values
DataFields  = "SHIP_FLAG"             :@VM:SQL_Format(Rec(PM_SPEC_SHIP_FLAG$),  "BIT"):@FM
DataFields := "GRADE"                 :@VM:SQL_Format(Rec(PM_SPEC_GRADE$),      "STR"):@FM
DataFields := "SCRIBE"                :@VM:SQL_Format(Rec(PM_SPEC_SCRIBE$),     "STR"):@FM
DataFields := "GAN_RUN_ID"            :@VM:SQL_Format(Rec(PM_SPEC_GAN_RUN_ID$), "STR"):@FM

// Symbolics
DataFields := "CARR_SLOT_ID"          :@VM:SQL_Format({CARR_SLOT_ID},   "STR"):@FM
DataFields := "CARR_WFR_GRADE"        :@VM:SQL_Format({CARR_WFR_GRADE}, "STR"):@FM
DataFields := "CURR_CASS_ID"          :@VM:SQL_Format({CURR_CASS_ID},   "STR"):@FM
DataFields := "CURR_LOC_ID"           :@VM:SQL_Format({CURR_LOC_ID},    "STR"):@FM
DataFields := "CURR_SLOT_ID"          :@VM:SQL_Format({CURR_SLOT_ID},   "STR"):@FM
DataFields := "CURR_TOOL"             :@VM:SQL_Format({CURR_TOOL},      "STR"):@FM
DataFields := "CUST_NAME"             :@VM:SQL_Format({CUST_NAME}[1,50],"STR"):@FM
DataFields := "CUST_NO"               :@VM:SQL_Format({CUST_NO},        "STR"):@FM
DataFields := "ELAP_TM"               :@VM:SQL_Format({ELAP_TM},        "STR"):@FM
DataFields := "EPI_STRUCTURE"         :@VM:SQL_Format({EPI_STRUCTURE},  "STR"):@FM
DataFields := "GAN_WFR_ID"            :@VM:SQL_Format({GAN_WFR_ID},     "STR"):@FM
DataFields := "POST_SLOT_DISP"        :@VM:SQL_Format({POST_SLOT_DISP}, "STR"):@FM
DataFields := "PRE_SLOT_DISP"         :@VM:SQL_Format({PRE_SLOT_DISP},  "STR"):@FM
DataFields := "RDS_NO"                :@VM:SQL_Format({RDS_NO},         "STR"):@FM
DataFields := "REACT_TYPE"            :@VM:SQL_Format({REACT_TYPE},     "STR")

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

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

// PM_SPEC_LOGS
If Ans EQ "" AND Rec(PM_SPEC_LOC_DTM$) NE "" then
        
    // List of data fields and their values
    MvFields  = "LOC_DTM"  :@FM:SQL_Format(Rec(PM_SPEC_LOC_DTM$),   "DATETIME"):@RM
    MvFields := "LOC_BY"   :@FM:SQL_Format(Rec(PM_SPEC_LOC_BY$),    "STR"):@RM
    MvFields := "LOC_EVENT":@FM:SQL_Format(Rec(PM_SPEC_LOC_EVENT$), "STR"):@RM
    MvFields := "SLOT_ID"  :@FM:SQL_Format(Rec(PM_SPEC_SLOT_ID$),   "STR"):@RM
    MvFields := "RUN_LOC"  :@FM:SQL_Format(Rec(PM_SPEC_RUN_LOC$),   "STR"):@RM
    MvFields := "NCR_NO"   :@FM:SQL_Format(Rec(PM_SPEC_NCR_NO$),    "STR"):@RM
    MvFields := "TW_USE"   :@FM:SQL_Format(Rec(PM_SPEC_TW_USE$),    "STR"):@RM
    MvFields := "DISP"     :@FM:SQL_Format({DISP},                 "STR")     
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "PM_SPEC_LOGS", Keys, MvFields)
    
end

Return Ans
