Sunday, April 18, 2010

Evaluating an expression

Today, I learned a new 11g Release 1 and above 'trick' that I wasn't aware of. This is pretty cool.

scott@ORCL> select * from v$version;

BANNER
-------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE    11.1.0.6.0      Production
TNS for 32-bit Windows: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production

scott@ORCL> variable x varchar2(30);
scott@ORCL> exec :x := '(1+2)/3';

PL/SQL procedure successfully completed.

scott@ORCL> SELECT xmlquery( REPLACE(:x,'/','div') returning content).getnumberval() AS result
  2  FROM dual;

    RESULT
----------
         1

The XQuery was there in Oracle 10g but it doesn't support literals.

scott@RASDEV> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production

scott@RASDEV> variable x varchar2(30);
scott@RASDEV> exec :x := '(1+2)/3';

PL/SQL procedure successfully completed.

scott@RASDEV> SELECT xmlquery( REPLACE(:x,'/','div') returning content).getnumberval() AS result
  2  FROM dual;
SELECT xmlquery( REPLACE(:x,'/','div') returning content).getnumberval() AS result
                 *
ERROR at line 1:
ORA-19102: XQuery string literal expected

http://www.xquery.com/tutorials/guided-tour/xquery-operators.html

No comments:

Post a Comment