Question

I have the following SQL*Plus code:

COLUMN x NEW_VALUE x2
BEGIN
   -- ...
   EXECUTE IMMEDIATE 'SELECT ''a'' x FROM dual';
   EXECUTE IMMEDIATE 'SELECT colname x FROM some_table WHERE (some_condition)';
   -- ...
END;

at this point, &x2 is still empty, and colname is NOT NULL. Why wasn't it set, to 'a' if nothing else?

Was it helpful?

Solution

COLUMN is an SQL*Plus client construct, and the dynamic SQL is running inside the PL/SQL engine, so there's no real connection. I'm not sure where you'd document something not applying.

The closest I can think of for this - assuming you're doing this to have the results of your dynamic SQL available to plan SQL statements later in a script - is using bind variables instead:

var x varchar2(30);

BEGIN
    EXECUTE IMMEDIATE 'SELECT ''a'' x FROM dual' INTO :x;
END;
/

select :x from dual;

You can use :x anywhere you'd have used &x2, but bear in mind that because it's a bind variable you don't need to put it in single quotes (if it's a varchar) when you use it; if you did this:

select ':x' from dual;

... then you'd back the literal string :x, not a.

Edited to add

If you particularly need the & form, you can do a further step (untested, but can't see why not):

column x2 new_value x3
select :x as x2 from dual;

... and you'll have &x3 available.

OTHER TIPS

After hours of testing and searching (fruitlessly) on the web, this is the best I can come up with: EXECUTE IMMEDIATE runs in its own bubble universe, at least as far as the COLUMN support is concerned. It doesn't even see it, and won't no matter what you do. No idea why this is, or why it isn't documented anywhere (at least that I can find).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top