Hi,
We're using Oracle 12c (PL/SQL Release 12.1.0.2.0 - Production)
I'm testing Dynamic SQL in an Anonymous block and for some reason when I replace the table names with bind variables the code shows and error:
Here's the code:
set serveroutput on
DECLARE
v_sql_stmt VARCHAR2(1000):= NULL;
v_exists NUMBER;
v_table_name_tmp Varchar2(100);
v_table_name Varchar2(100);
BEGIN
v_table_name_tmp := 'SLD_LOAN_CLTRL_EAD_TMP';
v_table_name := 'SLD_LOAN_CLTRL_EAD';
v_exists := 0;
--SELECT 1 into v_exists FROM DBA_TABLES WHERE TABLE_NAME= 'SLD_LOAN_CLTRL_EAD_TMP';
v_sql_stmt :=
'SELECT 1
FROM DBA_TABLES
WHERE TABLE_NAME = :b_table_name_tmp';--sld_loan_cltrl_ead_temp
EXECUTE IMMEDIATE v_sql_stmt
INTO v_exists USING v_table_name_tmp;
dbms_output.put_line('We made it to here!');
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_sql_stmt :=NULL;
v_sql_stmt :=
'CREATE TABLE :b_table_name_tmp
AS
SELECT * from :b_table_name
WHERE 1=2';
EXECUTE IMMEDIATE v_sql_stmt
USING v_table_name_tmp, v_table_name;
WHEN OTHERS THEN
dbms_output.put_line('Error in CREATE STATEMENT: '||SQLERRM);
END;
The DBMS OUTPUT from the Exceptions block gives this:
We made it to here!
Error in CREATE STATEMENT: ORA-00904: "SLD_LOAN_CLTRL_EAD_TMP": invalid identifier
The error in the procedure is this:
Error at line 2
ORA-00903: invalid table name
ORA-06512: at line 35
ORA-01403: no data found
Script Terminated on line 2 of \\NAHomeWFS02\Home03$\rs818\My Documents\USB 4G\HFIT\Oracle Utiliy SQL\Working Dynamic SQL with Bind variables.sql.
I've compared this with a very similar block of Dynamic SQL running in a procedure and I found no difference. My only thought is that bind variables may only work when the Dynamic SQL is called form a procedure and not an Anonymous block.
I'm curious if I missed something. Can anyone confirm this? Otherwise I'm not sure why the above block won't run when I introduce the bind variables.
Thanks!