I am programming in Visual Studio, but I need to perform validation on mySQL (v8.0.28) table and I guess it would be faster if it will be performed as procedure on server. Unfortunatelly, my MySQL programming skills are very limited.
This is equivalent of my table:
NodeID | NodeLevel | NodeParent | NodeValue | NodeValid |
---|---|---|---|---|
0 | 0 | root | NULL | 0 |
1 | 1 | 0 | NULL | 0 |
2 | 2 | 1 | NULL | 0 |
3 | 3 | 2 | 2023-03-03 | 1 |
4 | 0 | root | NULL | 0 |
5 | 1 | 4 | NULL | 0 |
6 | 2 | 5 | NULL | 0 |
7 | 3 | 6 | 2023-03-03 | 1 |
8 | 0 | root | NULL | 0 |
9 | 1 | 8 | NULL | 0 |
10 | 1 | 8 | NULL | 0 |
It works like Treeview nodes and I am able to validate state of bottom level nodes by this line:
UPDATE `bs`.`valitable`
SET `NodeValid`='1'
WHERE `NodeLevel`='3' AND `NodeValue` > '2023-02-22';
How I can walk through nodes where NodeLevel
='2', because I need to check if all children are Valid = 1 (then Parent Valid = 1), otherwise parent Valid = 0.
I found some WHILE...DO, FOR loops, Cursors, but I am not able to write correct syntaxe to perform this action correctly. Or - is there other way how to do this?
I tried to do this in Visual Studio, but its too slow because it require to load entire table, proceed and save back. As the table grows, its unusable so I hope mySQL procedure could speed up the process, because it could run directly from server.