Skip to content
Advertisement

MySQL – While Exists from a snippet throws error

I was reading through this page https://www.sqlteam.com/articles/more-trees-hierarchies-in-sql (great article on managing hierarchical data in SQL) and following along in MySQL and was trying to run this SQL snippet.

Except I get the following error:

You have an error in your SQL syntax; it seems the error is around: ‘WHILE EXISTS ( SELECT * FROM Tree WHERE Depth Is Null ) UPDATE’ at line 1

I’ve tried adding in BEGIN, END, END WHILE etc from suggestions in While exists in mysql but still not working.

You can try and run the above snippet in an SQL validator (https://www.eversql.com/sql-syntax-check-validator/ is a good free online one) and see the error.

I’m looking for suggestions on why the above snippet doesn’t work from the blog / what can be changed so the same result is achieved.

Update – Adding in more data

Thanks @danblack for the suggestion to add more data to make it a verifiable example.

My current table

What it should look like after the snippet is run

@danblack pointed out that the snippet is Microsoft SQL, not MySQL and should use a recursive CTE to do this. This is my attempt but I still can’t figure it out.

Advertisement

Answer

In MySQL-8.0:

| version() |
| :-------- |
| 8.0.13    |
Node | ParentNode | EmployeeID | Depth | Lineage      
:--- | :--------- | ---------: | ----: | :------------
100  | null       |       1001 |     0 | /            
101  | 100        |       1002 |     1 | /100/        
102  | 101        |       1003 |     2 | /100/101/    
103  | 102        |       1004 |     3 | /100/101/102/
104  | 102        |       1005 |     3 | /100/101/102/
105  | 102        |       1006 |     3 | /100/101/102/
Node | ParentNode | EmployeeID | Depth | Lineage      
:--- | :--------- | ---------: | ----: | :------------
100  | null       |       1001 |     0 | /            
101  | 100        |       1002 |     1 | /100/        
102  | 101        |       1003 |     2 | /100/101/    
103  | 102        |       1004 |     3 | /100/101/102/
104  | 102        |       1005 |     3 | /100/101/102/
105  | 102        |       1006 |     3 | /100/101/102/

mysql8.0 db<>fiddle here

MariaDB does not yet supported UPDATE in CTEs

However it can be performed with a temporary table like:

Node | ParentNode | EmployeeID | Depth | Lineage      
:--- | :--------- | ---------: | ----: | :------------
100  | null       |       1001 |     0 | /            
101  | 100        |       1002 |     1 | /100/        
102  | 101        |       1003 |     2 | /100/101/    
103  | 102        |       1004 |     3 | /100/101/102/
104  | 102        |       1005 |     3 | /100/101/102/
105  | 102        |       1006 |     3 | /100/101/102/

MariaDB-10.3 db<>fiddle here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement