0

Consider the following scenario. I have a bunch of user defined functions, f_i, that create, populate, and use a local temporary table with the same name res:

create function f_i()
returns table( … )
begin
       # Result stats table.
       create local temporary table res(
             param1 int,
             param2 int
       );

       /* Populate res */
       /* Use res in some queries */

       return table(
             /* Query involving res */
       );
end;

My first question is: Does create local temporary table x create a table that is disposed/dropped as soon as the function where it was declared finishes execution? I assumed this was the case, but I’m now skeptical as we are running into all sorts of concurrency issues that point to the res table not being cleared out properly from memory.

This takes me to the second question: How do I drop a temporary table if such table exists? The reason for this is to prevent table create exceptions (which we are currently running into). I’d like to achieve something along these lines:

create function f_i()
returns table( … )
begin
       # Drop local temporary table if such table exists.
       drop local temporary table res;         <-------- How do I do this in MonetDB?

       # Result stats table.
       create local temporary table res(
             param1 int,
             param2 int
       );

       /* Populate res */
       /* Use res in some queries */

       return table(
             /* Query involving res */
       );
end;

Where, as you can see, I want to explicitly drop the local temporary table res if such exists. If I leave the local temporary markers in the drop statement, MonetDB complains that it found unexpected LOCAL or TEMPORARY tokens. I wouldn’t like to remove these markers because a statement like:

drop table res;

will drop any table in the default function schema with that name.

Any help and hints are well appreciated!

  • did you find a solution to this yet? – Alam Aug 31 '17 at 19:54
  • @Alam I tried with `drop table tmp.yourTableName` and it works, as long as you don't do it within a user defined function. – 영민 카이 앤절 Sep 02 '17 at 21:05
  • thanks for the reply, but I really need to do it in my function\procedure, it doesn't allow it in either of them even though the documentation https://www.monetdb.org/Documentation/Manuals/SQLreference/Procedures says that its possible in a procedure, The error says it an "invalid flow control statement". – Alam Sep 03 '17 at 03:25
  • @Alam, we faced the same inconvenient limitation – 영민 카이 앤절 Sep 04 '17 at 18:49

1 Answers1

0

try following?

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL   DROP TABLE #Temp;
ViKiNG
  • 1,294
  • 2
  • 19
  • 26
  • Unfortunately, MonetDB doesn't have such a function like OBJECT_ID. There's no information either as where to find local temporary tables created in a user-defined function. If I could tell where information about local temporary tables is stored, I could query that table and use the homologue to `if` to drop it. My question remains: is it possible to drop a local temporary table? – 영민 카이 앤절 Mar 21 '17 at 00:02