function DBwriteKL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn,NodoKlinoLink)

l = ListaDate;
    
for ii=1:l
    tablename = 'ELABDATADISP';
    checkT = isnan(cell2mat(DATAinsert(ii,9)));
    if checkT == 1 % il vettore Temperatura è NaN
        if strcmp(NodoKlinoLink(1,4),'ADC') || strcmp(NodoKlinoLink{1,4} ,'null') || isempty(NodoKlinoLink{1,4}) == 1
            comando = ['INSERT INTO ' tablename ' (UnitName,ToolNameID,NodeNum,EventDate,EventTime,XShift,YShift,ZShift,calcerr)' ...
                ' VALUES( ' DATAinsert(ii,1) ',' DATAinsert(ii,2) ',' DATAinsert(ii,3) ',' DATAinsert(ii,4) ','...
                DATAinsert(ii,5) ',' DATAinsert(ii,6) ',' DATAinsert(ii,7) ',' DATAinsert(ii,8) ',' DATAinsert(ii,10)...
                '), ON DUPLICATE KEY UPDATE XShift=' DATAinsert(ii,6) ', YShift=' DATAinsert(ii,7) ', ZShift=' DATAinsert(ii,8)...
                ', calcerr=' DATAinsert(ii,10)];
        elseif strcmp(NodoKlinoLink{1,4} ,'Gradi')
            comando = ['INSERT INTO ' tablename ' (UnitName,ToolNameID,NodeNum,EventDate,EventTime,XShift,YShift,calcerr)' ...
                ' VALUES( ' DATAinsert(ii,1) ',' DATAinsert(ii,2) ',' DATAinsert(ii,3) ',' DATAinsert(ii,4) ','...
                DATAinsert(ii,5) ',' DATAinsert(ii,6) ',' DATAinsert(ii,7) ',' DATAinsert(ii,10)...
                '), ON DUPLICATE KEY UPDATE XShift=' DATAinsert(ii,6) ', YShift=' DATAinsert(ii,7) ', calcerr=' DATAinsert(ii,10)];
        end
    else
        if strcmp(NodoKlinoLink(1,4),'ADC') || strcmp(NodoKlinoLink{1,4} ,'null') || isempty(NodoKlinoLink{1,4}) == 1
            comando = ['INSERT INTO ' tablename ' (UnitName,ToolNameID,NodeNum,EventDate,EventTime,XShift,YShift,ZShift,T_node,calcerr)' ...
                ' VALUES( ' DATAinsert(ii,1) ',' DATAinsert(ii,2) ',' DATAinsert(ii,3) ',' DATAinsert(ii,4) ','...
                DATAinsert(ii,5) ',' DATAinsert(ii,6) ',' DATAinsert(ii,7) ',' DATAinsert(ii,8) ',' DATAinsert(ii,9) ', ' DATAinsert(ii,10)...
                '), ON DUPLICATE KEY UPDATE XShift=' DATAinsert(ii,6) ', YShift=' DATAinsert(ii,7) ', ZShift=' DATAinsert(ii,8)...
                ', T_node = ' DATAinsert(ii,9) ', calcerr=' DATAinsert(ii,10)];
        elseif strcmp(NodoKlinoLink{1,4} ,'Gradi')
            comando = ['INSERT INTO ' tablename ' (UnitName,ToolNameID,NodeNum,EventDate,EventTime,XShift,YShift,T_node,calcerr)' ...
                ' VALUES( ''' string(DATAinsert(ii,1)) ''',''' string(DATAinsert(ii,2)) ''',''' string(DATAinsert(ii,3)) ''',''' string(DATAinsert(ii,4)) ''','''...
                string(DATAinsert(ii,5)) ''',''' string(DATAinsert(ii,6)) ''',''' string(DATAinsert(ii,7)) ''',''' string(DATAinsert(ii,9)) ''', ''' string(DATAinsert(ii,10))...
                ''') ON DUPLICATE KEY UPDATE XShift=''' string(DATAinsert(ii,6)) ''', YShift=''' string(DATAinsert(ii,7)) ...
                ''', T_node=''' string(DATAinsert(ii,9)) ''', calcerr=''' string(DATAinsert(ii,10)) ''' '];
        end
    end
    comando = strjoin(comando);
    curs = exec(conn,comando);
    curs = fetch(curs);
    disp(curs.Data)
end

end
