A lot of our SQL statements currently rely on temp tables. In Postgres and SQL Server these have very nice properties: they’re only visible to the current user, are automatically dropped when the session ends, and can be created even when the user has no write access. In Oracle, temp tables are basically permanent tables, with the only difference that the data inside the table is only visible to the current user.
Our current solution is to
- Always set the current schema to somewhere where the user has write access (the results schema they specified), and adding explicit schema references to all other tables (e.g. @cdm_schema.dbo.person).
- Add truncate table and drop table statements for temp tables at the beginning and end of the SQL code.
- Generate random temp table names so they cannot conflict with the temp tables of another user (haven’t done that yet)
Of course this sucks. Trying to read up on why Oracle is so bad with temp tables, it seems they’re advocating the use to CTEs so the optimizer gets a shot at being cleverer than the user. I will see if I can add CTEs to SqlTranslate (shouldn’t be a problem), but I have two reservations about them:
- I don’t share Oracle’s confidence in the optimizer
- Using CTEs makes code that much harder to debug
Any thoughts?