VARIABLE
declares a bind variable, so you need to prefix it with a colon when you refer to it:
VARIABLE liab NUMBER;
exec select nvl(LIAB_ID,-1) into :liab FROM ...
SPOOL c:\query.SQL
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' FROM DUAL;
spool off
You could also do this with a substitution variable instead of a bind variable:
column liab new_value g_liab
select nvl(LIAB_ID,-1) as liab FROM ...
SPOOL c:\query.SQL
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = &g_liab;' FROM DUAL;
spool off
The column x new_value y
syntax defines a substitution variable &y
whose value is set from a column called x
in a subsequent query.
In both cases SQL Developer will put both the header and the command you ran into the spool file, so you'd see this:
> select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' from dual
'SELECT*FROMGETB_UTILSWHERELIAB_ID='||:LIAB||';'
----------------------------------------------------------------------------------
SELECT * FROM GETB_UTILS WHERE LIAB_ID = 1;
> spool off
You can add set heading off
and set echo off
, but if you run it directly from an SQL Worksheet you'd still end up with:
> select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' from dual
SELECT * FROM GETB_UTILS WHERE LIAB_ID = 1;
To get rid of the echo'd command you'd need to save this as a script and then run that from another worksheet, as was the case with your previous question. So if you had a script called liab.sql
containing:
VARIABLE liab NUMBER;
set feedback off
exec select nvl(LIAB_ID,-1) into :liab FROM ...
set echo off
set heading off
SPOOL c:\query.SQL
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' FROM DUAL;
spool off
... and then ran that with
@liab.sql
... the query.sql
file would contain just:
SELECT * FROM GETB_UTILS WHERE LIAB_ID = 1;