Query validation based field update

Asked

Viewed 117 times

0

I created a source to update a field within a table based on a query validation, ask it works in debug, but for some reason it doesn’t go through the array, and also doesn’t update any record, even though it has 6000 to update.

User Function IM06SCX01()

Private oProcess := Nil

    FWMsgRun(, {|oSay| U_IM06SC01()}, "Processando Registros", "Processando")

Return()


User Function IM06SC01()

Local cQuery    := ""
Local aCols     := {}
Local cAlias    := "SE2"

Private cPerg   := "IM06SC01  "



/*PRIVATE mv_par01
PRIVATE mv_par02
PRIVATE mv_par03
PRIVATE mv_par04
*/

ValidPerg(cPerg)
If !Pergunte(cPerg,.T.)
    Return
EndIf 

        cQuery := " SELECT * "                                                                 + CRLF
        cQuery += " FROM "+RetSQLName("SE2")+" SE2  "                                          + CRLF
        cQuery += " WHERE D_E_L_E_T_ = ' '  "                                                  + CRLF
        cQuery += " AND E2_EMISSAO <> E2_EMIS1  "                                              + CRLF
        cQuery += " AND E2_FILIAL  BETWEEN '"+  mv_par01 +"' AND '"+  mv_par02 +"'  "          + CRLF
        cQuery += " AND E2_EMISSAO BETWEEN '" + Dtos(mv_par03)+"' AND  '"  + Dtos(mv_par04)+"'"+ CRLF   
        TCQUERY cQuery NEW ALIAS (cAlias)

        (cAlias)->(DbGoTop())    

        Do While !(cAlias)->(Eof())

            aAdd(aCols,{.F.,;
            (cAlias)->E2_FILIAL,;           
            (cAlias)->E2_NUM,;
            (cAlias)->E2_TIPO,;
            (cAlias)->E2_FORNECE,;
            (cAlias)->E2_LOJA})
            (cAlias)->(dbSkip())

        EndDo

        (cAlias)->(DbGoTop())

        DbSelectArea("SE2")
        SE2->(dbSetOrder(1))
        SE2->(dbGoTop())

        For nx := 1 To Len(aCols)               
            If SE2->(dbSeek(aCols[nx][2]+aCols[nx][3]+aCols[nx][4]+aCols[nx][5]+aCols[nx][6]))
                    RecLock("SE2",.F.)
                    SE2->E2_EMIS1 := SE2->E2_EMISSAO                            
                    SE2->(MsUnlock())   

                    nCont++
            Endif


        Next nx

        If nCont > 0
            MsgInfo(cValtoChar(nCont) + " registros processados.", "TOTVS")
        Else
            MsgInfo("Nenhum registro processado","TOTVS")
        Endif

Return 

//*--------------------------------------------------------------------------------------------------------------------

Static Function ValidPerg(cPerg)

Local aArea := GetArea()

cPerg := PADR(cPerg,10)

PutSx1(cPerg,"01","Filial de      ","","","mv_ch1","C",06,0,0,"G","","","","","mv_par01","","","","","","","","","","","","","","","","","","","","","","")
PutSx1(cPerg,"02","Filial ate     ","","","mv_ch2","C",06,0,0,"G","","","","","mv_par02","","","","","","","","","","","","","","","","","","","","","","")
PutSx1(cPerg,"03","Data de        ","","","mv_ch3","D",08,0,0,"G","","","","","mv_par03","","","","","","","","","","","","","","","","","","","","","","")
PutSx1(cPerg,"04","Data Ate       ","","","mv_ch4","D",08,0,0,"G","","","","","mv_par04","","","","","","","","","","","","","","","","","","","","","","")

RestArea(aArea)

Return
  • Check the error logs, something pops up ? If yes, edit the question by adding the log

  • 1

    Returns only : Output value: 1

1 answer

1

I think your program is using the alias "SE2" for Query ... your program opens the search query in Alias SE2, saved in the variable cAlias, keeps the Query open, and ends up trying to do the search and update in the Query, not in the table "SE2" system real.

Try using a different alias for the Query, and close it after reading the data and feeding the array. Then, select the table alias SE2 to perform searches and updates.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.