2
DECLARE dyn_stmt VARCHAR2(200);
b1 boolean;
FUNCTION f(x INTEGER) RETURN boolean AS
BEGIN
RETURN FALSE;
END f;
BEGIN
dyn_stmt := 'BEGIN :b := f(5); END;';
EXECUTE IMMEDIATE dyn_stmt USING OUT b1; --line 9
b1:=f(5);   --line 10
END;

Commenting line 10 throws error whereas Commenting line 9 works fine. Why ? According to error "expressions have to be of SQL types" and AFAIK PL/SQL supports boolean data type. Then, what's the problem here ?

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • 1
    What version of Oracle are you using? I believe this will be version-dependent behavior. The underlying issue is that `boolean` is not a SQL data type, it's just a PL/SQL data type. – Justin Cave Apr 28 '15 at 04:46
  • I'm using 11g, updated tag as well. – Ravi Apr 28 '15 at 04:47
  • I think you've mixed the lines; `--line 9` will throw `PLS-00457: expressions have to be of SQL types` but `--line 10` works as expected. I think the error text pretty much explains the problem. Why you did left it out from the question ? – user272735 Apr 28 '15 at 04:50
  • That's what I said... when I comment out `line 9` then it works fine, but when I comment `line 10` then it throws error. Does my question interpreted something else :S ? – Ravi Apr 28 '15 at 04:52
  • Oh, sorry I interpreted `commenting line 10 throws error` meaning when one comment out line 10 then an error is thrown. (I'm not native English speaker.) – user272735 Apr 28 '15 at 04:54
  • Anyway the problem was already addressed by @JustinCave. See [EXECUTE IMMEDIATE Statement](http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/dynamic.htm#LNPLS01115): _"The EXECUTE IMMEDIATE statement is the means by which native dynamic SQL processes most dynamic SQL statements."_ and _"No bind variable has a data type that SQL does not support (such as BOOLEAN in Example 7-2)."_. – user272735 Apr 28 '15 at 04:57
  • @user272735 Me too :) ;) – Ravi Apr 28 '15 at 04:57

1 Answers1

1

As per the Oracle Documentation:

This specifically applies to dynamic PL/SQL (and SQL too):

At run time, bind arguments replace corresponding placeholders in the dynamic string. Every placeholder must be associated with a bind argument in the USING clause and/or RETURNING INTO clause. You can use numeric, character, and string literals as bind arguments, but you cannot use Boolean literals (TRUE, FALSE, and NULL). To pass nulls to the dynamic string, you must use a workaround. See "Passing Nulls to Dynamic SQL".

This does not apply to Dynamic PL/SQL (only SQL), but continuing for completeness:

Dynamic SQL supports all the SQL datatypes. For example, define variables and bind arguments can be collections, LOBs, instances of an object type, and refs. Dynamic SQL does not support PL/SQL-specific types. For example, define variables and bind arguments cannot be BOOLEANs or index-by tables. The only exception is that a PL/SQL record can appear in the INTO clause.

YoYo
  • 9,157
  • 8
  • 57
  • 74