Is it possible to have conditional compilation in Oracle, where the condition is the existence of a database object (specifically, a table or view or synonym)? I’d like to be able to do something like this:
sp_some_procedure is
$IF /*check if A exists.*/ then
/* read from and write to A as well as other A-related non-DML stuff...*/
$ELSE /*A doesn't exist yet, so avoid compiler errors*/
dbms_output.put_line('Reminder: ask DBA to create A!')
$ENDIF
end;
No – that is not possible… but if you create a stored procedure referencing a non-existent DB object and try to compile it the compilation will show errors… the stored procedure will be there but “invalid”… and the compilation errors are accessible for the DBA whenever he looks at it… so I would just go ahead and create all needed stored procedures, if any compilation errors arise ask the DBA (sometimes the object exists but the stored procedure need permissions to access it…)… after the reason for the error(s) is fixed you can just recompile the stored procedure (via
ALTER PROCEDURE MySchema.MyProcName COMPILE;) and all is fine…IF you don’t want code to be there you can just
DROPthe strored procedure and/or replace is viaCREATE OR REPLACE… withdbms_output.put_line('Reminder: ask DBA to create A!')in the body.The only other alternative is as kevin points out
EXECUTE IMMEDIATEwith properEXCEPTIONhandling…