r/SQL • u/theseyeahthese • Apr 13 '19
MS SQL [MS SQL] When to use a CTE vs. a temp table?
Title is pretty self-explanatory. I work with some developers that love CTE’s, while I like to break up similar queries into temp tables. I’ve fixed major performance issues in stored procedures by simply turning a CTE into a temp table. Are there any benefits to using a CTE instead of a temp table, besides recursive CTE’s? If it matters, I’m referring to temp tables as the “#” tables, but sometimes I’ll substitute that for a “real” table that I just end up dropping at the end of a procedure. I have never really used “Variable” temp tables, so feel free to add those into the mix of your answer if they respond differently than the other 3 table object types I’ve mentioned.