From what I understand, table expressions do not persist, meaning every reference to it in the outer code is a repeated call to the definition. This is where table variable/temp tables comme into the picture, to persist the table and avoid re-evaluating every single time. It seems to me table variables/temp tables are better choice to avoid repetition. I made an anology to programming, where instead of calling method
myMethod()
each time, I would rather assign a variable to it
int res = myMethod()
to avoid calling myMethod() multiple times. myMethod()
is analogous to table expression and res is table variable/temp table.
So why would we use table expressions? In which scenarios must we use table expressions instead of the other two? And vice versa?
Thank you.