Posted to tcl by schelte at Wed May 08 15:19:51 GMT 2024view raw

  1. # I have a table with a sequence of name changes.
  2. # I want to translate from any past name to the current name.
  3.  
  4. sqlite3 db ""
  5. db eval {
  6. create table rename (old text primary key, new text unique);
  7. insert into rename values \
  8. ('orig', 'name1'), \
  9. ('name1', 'name2'), \
  10. ('name2', 'name3'), \
  11. ('name3', 'final');
  12. }
  13.  
  14. set name orig
  15.  
  16. db eval {
  17. with recursive temp(name)
  18. as (values($name) union
  19. select new from rename, temp
  20. where temp.name = rename.old)
  21. select name from temp
  22. } {
  23. puts $name
  24. }
  25.  
  26. # This prints:
  27. # orig
  28. # name1
  29. # name2
  30. # name3
  31. # final
  32. #
  33. # How to adjust the query to only get the final result?