Encountered an error while trying to use bind variable in dynamic sql for table deletion

StackOverflow https://stackoverflow.com/questions/23636064

  •  21-07-2023
  •  | 
  •  

Pergunta

So, i'm trying to write dynamic sql with bind variable to delete table. Maybe i need to add somewhere qoutes or what? I have tried adding them but it didn't change. I have written tables creation almost in the same way (using bind variables too) and it works fine. I have no idea why deletion doesn't work :/ Therefore, i'm asking you guys to help me to figure out what's wrong. :)

Sorry for links, but in this way the code will be more readable.

Here's the code:

PROCEDURE create_tables IS

   .............
  ...more code...
   .............

33   PROCEDURE empty_table(in_table_name VARCHAR2) IS
34     l_statement VARCHAR2(100) := 'DELETE :bind_table_name';
35   BEGIN
36     EXECUTE IMMEDIATE l_statement
37       USING in_table_name;    
38   END;
39
40 BEGIN  
41   FOR i IN 1..g_tables.COUNT LOOP
42     IF check_existence(g_tables(i)) = 0
43       THEN
44         create_table(g_tables(i));
45       ELSE
46         empty_table(g_tables(i));
47     END IF;
48   END LOOP;
49 END create_tables;

And here's the error message:

Error report -
ORA-00903: invalid table name
ORA-06512: at "LATVYT.THANDLER", line 36
ORA-06512: at "LATVYT.THANDLER", line 46
ORA-06512: at line 4
00903. 00000 -  "invalid table name"
*Cause:    
*Action:

This is how g_tables look:

TYPE TLIST IS TABLE OF VARCHAR2(30);
g_tables TLIST := TLIST('OPERACIJA', 'PRODUKTAS', 'PRODUKTO_OPERACIJA', 'PLANAS', 'DARBUOTOJAS', 'UZDUOTIS');
Foi útil?

Solução

Deleting a table is a data definition language (DDL) operation. Parameter binding is not supported for DDL. Even in an INSERT statement, you cannot use parameter binding to replace the table name.

Use simple string concatenation:

PROCEDURE empty_table(in_table_name VARCHAR2) IS
     l_statement VARCHAR2(100) 
BEGIN
   l_statement := 'DELETE ' || in_table_name;
   EXECUTE IMMEDIATE l_statement
END;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top