Translate

Monday, September 20, 2021

TSQL - Simple Recursive Example

I needed to change a field in a table that holds the tree structure for menues in Unit4 ERP. The fields involved were the parent_menu_id, menu_id, and client. So parent_menu_id would be the folder, menu_id would be the folders or menu item, and client is the field to be updated. The paren_menu_id at top of the tree is REP05. This is the update query.

WITH RecQry AS
(
    SELECT aagM.menu_id
      FROM aagmenu aagM where parent_menu_id = 'REP05'
    UNION ALL
    SELECT Child.menu_id
      FROM RecQry PARENT, aagmenu CHILD
        where CHILD.parent_menu_id = PARENT.menu_id
)
update aagmenu
set client = '*'
where menu_id in
(
select menu_id from RecQry
)