Compile function Copy_TW_USE_Record_To_SQL(Connection, Key, Record)

/*****************************************************************************\
	Copies the given TW_USE record to the MSSQL database.
	
	History
	-------
	06/29/2010    KRF   Original Programmer
\*****************************************************************************/

$insert TW_USE_EQUATES

Declare function SQL_Write, SQL_Write_Mv, SQL_Format
Ans = ""

// Parse record into a dimensioned array for speed
Dim Rec(20)
MatParse Record into Rec

// List of key names and their values
Keys  = "MET_NO":@VM:SQL_Format(Field(Key, "*", 1), "INT"):@FM
Keys := "TW_NO" :@VM:SQL_Format(Field(Key, "*", 2), "INT")

// List of data fields and their values
DataFields  = "TW_QTY"         :@VM:SQL_Format(Rec(TW_USE_TW_QTY$),          "INT"):@FM
DataFields := "TW_USE_CODE"    :@VM:SQL_Format(Rec(TW_USE_TW_USE_CODE$),     "STR"):@FM
DataFields := "[DESC]"         :@VM:SQL_Format(Rec(TW_USE_DESC$),            "STR"):@FM
DataFields := "TOOL"           :@VM:SQL_Format(Rec(TW_USE_TOOL$),            "STR"):@FM
DataFields := "TW_TYPE"        :@VM:SQL_Format(Rec(TW_USE_TW_TYPE$),         "STR"):@FM
DataFields := "SIGNATURE"      :@VM:SQL_Format(Rec(TW_USE_SIGNATURE$),       "STR"):@FM
DataFields := "SIG_DTM"        :@VM:SQL_Format(Rec(TW_USE_SIG_DTM$),         "DATETIME"):@FM
DataFields := "HRS_SINCE_PROD" :@VM:SQL_Format(Rec(TW_USE_HRS_SINCE_PROD$),  "DEC", "0"):@FM
DataFields := "HRS_ESC"        :@VM:SQL_Format(Rec(TW_USE_HRS_ESC$),         "DEC", "0"):@FM
DataFields := "CUST_TW_PART_NO":@VM:SQL_Format(Rec(TW_USE_CUST_TW_PART_NO$), "STR"):@FM

// Symbolics
DataFields := "CUST_NAME"     :@VM:SQL_Format({CUST_NAME}[1,50],"STR"):@FM
DataFields := "CUST_NO"       :@VM:SQL_Format({CUST_NO},        "STR"):@FM
DataFields := "LS_ID"         :@VM:SQL_Format({LS_ID},          "STR"):@FM
DataFields := "PS_NO"         :@VM:SQL_Format({PS_NO},          "STR"):@FM
DataFields := "RDS_NO"        :@VM:SQL_Format({RDS_NO},         "STR"):@FM
DataFields := "REACTOR"       :@VM:SQL_Format({REACTOR},        "STR"):@FM
DataFields := "REACT_TYPE"    :@VM:SQL_Format({REACT_TYPE},     "STR"):@FM
DataFields := "SIG_DT"        :@VM:SQL_Format({SIG_DT},         "DATE"):@FM
DataFields := "SPEC_TYPE"     :@VM:SQL_Format({SPEC_TYPE},      "STR"):@FM
DataFields := "SUB_WFR_ORIENT":@VM:SQL_Format({SUB_WFR_ORIENT}, "STR"):@FM
DataFields := "SUB_WFR_TYPE"  :@VM:SQL_Format({SUB_WFR_TYPE},   "STR"):@FM
DataFields := "TW_CLASS"      :@VM:SQL_Format({TW_CLASS},       "STR"):@FM
DataFields := "TW_TYPE_DESC"  :@VM:SQL_Format({TW_TYPE_DESC},   "STR"):@FM
DataFields := "TW_USE_DESC"   :@VM:SQL_Format({TW_USE_DESC},    "STR"):@FM
DataFields := "WFR_SIZE"      :@VM:SQL_Format({WFR_SIZE},       "STR"):@FM
DataFields := "WO_NO"         :@VM:SQL_Format({WO_NO},          "STR"):@FM
DataFields := "ZONE"          :@VM:SQL_Format({ZONE},           "STR")

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

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

// TW_USE_WAFER_IDS
If Ans EQ "" AND Rec(TW_USE_WAFER_ID$) NE "" then
    
    // List of data fields and their values
    WaferIds = Rec(TW_USE_WAFER_ID$)
    
    Convert ",":@SVM to @VM:@VM in WaferIds
    MvFields = "WAFER_ID":@FM:SQL_Format(WaferIds, "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "TW_USE_WAFER_IDS", Keys, MvFields);
    
end

Return Ans
