During Development today, my following requirements: I need to find data from different tables, but the SQL statements are relatively fixed:
Select count (*) CNT from table_a where code = codevalue;
In the preceding SQL statement, only table_a and codevalue change. Therefore, we use the variable binding method:
As follows:
Select count (*) CNT from: 1 where code =: 2
The entire call is as follows:
Create or replace procedure Test (scode in varchar2, icount out number) is
Begin
Execute immediate 'select count (*) CNT from: 1 where code =: 2'
Into icount
Using 'gd _ side', scode;
End test;
But an ORA-00903 error was reported during execution: Invalid table name.
After the method is changed to the following, everything is normal:
Create or replace procedure Test (scode in varchar2, icount out number) is
Begin
Execute immediate 'select count (*) CNT from' | table_name | 'where code =: 1'
Into icount
Using scode;
End test;
The difference is that the table name does not use the binding method, but the | method.
Finally, find the resource and find that the table name cannot be bound, because during SQL parsing, you must first know which table to operate and check whether the user has the permission for this table, there may be other reasons, so the table name cannot be bound.