Infinite loop in Precedure

Asked

Viewed 82 times

0

I have a table Categoria with CategoriaId of Primary key and CategoriaPaiId Foreign key with self relationship in CategoriaId.

I need to create a procedure listing the category tree.

Assuming I have the categories with their respective subcategories:
Perishables > Beverages > Wines > Red.

On the record Booze, i need it to be returned in the variable &CategoriaSubCategoriaNome the following string:

Perishables > Beverages.

In the Red record, I need it to be returned in the variable &CategoriaSubCategoriaNome the following string:

Perishables > Beverages > Wines

That was the trial I imagined:

&isTrue = true
&isAchou = true
do while &isTrue = true
    if &isAchou = true
            &isAchou = false    
            for each
                where CategoriaId = &CategoriaPaiId
                &CategoriaSubCategoriaNome = &CategoriaSubCategoriaNome+ " > " + CategoriaNome
                &isAchou = true
            endfor
        if &isAchou = false
            return
        endif
    endif
enddo

But it’s entering an infinite loop. Any idea?

2 answers

0

I don’t really understand this syntax, but it seems to me that you’re not changing the value of &isTrue at no time.

At some point I would expect a &isTrue=false because the while condition is in this variable. sure?!

0

This precedent does not make the logic of reading the data hierarchically. It takes a data and returns only, looping if the data is found.

The right thing would be something like this:

&CategoriaID = 10 // Por exemplo. Você precisa informar o ID inicial.
&CategoriaSubCategoriaNome.SetEmpty()

Do 'Processa estrutura'

Sub 'Processa estrutura'
    For each
        Where CategoriaId = &CategoriaID
            If Not &CategoriaSubCategoriaNome.IsEmpty()
                &CategoriaSubCategoriaNome = !' > ' + &CategoriaSubCategoriaNome
            EndIf
            &CategoriaSubCategoriaNome = CategoriaNome.Trim() + &CategoriaSubCategoriaNome
            If Not CategoriaPaiId.IsNull()
                &CategoriaId = CategoriaPaiId // ID da categoria Pai desta
                Do 'Processa estrutura'
            EndIf
    EndFor
EndSub

Browser other questions tagged

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