1

I am using ESQL/C code to provide backend support for a UI, connecting to an Informix database. I am creating temp table inside my code. But, I guess that if multiple users use this UI at the same time then temp table might already exist in the database which can create problem. So, can someone suggest if I can create temp table with PID as suffix inside my ESQL/C code

create temp table tabname_PID (name char(10));

In shell script I generally use tabname_$$.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kundan Kumar
  • 1,974
  • 7
  • 32
  • 54

1 Answers1

2

You can create the table with the PID embedded in it, but it isn't necessary. Any temporary table is only visible in the session that creates it, so you can use the same table name in each session (separate but concurrently executing ESQL/C program) without any fear of conflict.

If, despite the reassurances that it is unnecessary, you still want to do it, then you'll have to PREPARE and EXECUTE (or DECLARE, OPEN, FETCH, CLOSE) the statements from a string:

snprintf(sql, sizeof(sql), "CREATE TEMP TABLE tabname_%d(name CHAR(10))", (int)getpid());
EXEC SQL PREPARE s FROM :sql;
EXEC SQL EXECUTE s;

or use EXECUTE IMMEDIATE (which is the obvious winner here):

EXEC SQL EXECUTE IMMEDIATE :sql;

You will also then have to prepare all the queries; one distinct advantage of using the fixed-name temporary table is that you don't have to prepare everything that references the temp table if you don't want to (though there are often advantages to using PREPARE etc).

You don't have to use $$ in shell scripts either, for the same reason — temporary tables are private to a session.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @Jonathan.....Thanks a lot !! Does it mean that if I use temp table in one termial with some processing going on and then open other terminal session there wont be any conflict ? – Kundan Kumar Feb 26 '13 at 21:37
  • 2
    Yes, it does mean that. It also means that if you have a single program and do `EXEC SQL CONNECT TO 'dbase' AS 'c1';` and then do `EXEC SQL CONNECT TO 'dbase' AS 'c2';`, you have two connections (and hence sessions) connected to the same database from the same client process, but the temporary tables for the session on connection `c1` are separate from the temporary tables for the session on connection `c2`. Temporary tables are private to the session that creates them. – Jonathan Leffler Feb 26 '13 at 21:51