Oracle 12c: several functions in a SELECT WITH WITH statement

select banner
from v$version
;


BANNER
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production
"CORE   12.1.0.2.0  Production"
TNS for Solaris: Version 12.1.0.2.0 - Production
NLSRTL Version 12.1.0.2.0 - Production

With the release of 12c, Oracle has added features that allow you to declare PL / SQL functions directly at the top of the SQL query (see https://oracle-base.com/articles/12c/with-clause-enhancements-12cr1 )

This can be a pretty handy feature, especially. on projects where you need to pull data from a database with user rights limited by SELECT statements.

A simple example:

with 
  function add_string(p_string in varchar2) return varchar2
  is
    --Function to add a string
    l_buffer varchar2(32767);
  begin
    l_buffer := p_string || ' works!';
    --
    return l_buffer;
    --
  end ; 
--
select add_string('Yes, it') as outVal
from dual
;

---------
OUTVAL
Yes, it works!

However, I have not yet been able to include several in the WITH clause:

with 
  function add_string(p_string in varchar2) return varchar2
  is
    --Function to add a string
    l_buffer varchar2(32767);
  begin
    l_buffer := p_string || ' works!';
    --
    return l_buffer;
    --
  end ; 
  --      
, function doesnt_it(p_string in varchar2) return varchar2
  is 
    l_buffer varchar2(32767);
  begin
    l_buffer := p_string || ' Doesnt it?';
    --
    return l_buffer;
    --
  end ; 
--
select add_string('Yes, it') as outVal
from dual
;

Throws out ORA-00928: missing SELECT keyword. Does anyone know if multi-function ads are allowed with this new feature, and if so, how can they be achieved?

+4
1

, . TOAD, sqlDeveloper sqlPlus.

, :

with 
  function add_string(p_string in varchar2) return varchar2
  is
    --Function to add a string
    l_buffer varchar2(32767);
  begin
    l_buffer := p_string || ' works!';
    --
    return l_buffer;
    --
  end ; 
  --      
  function doesnt_it(p_string in varchar2) return varchar2
  is 
    l_buffer varchar2(32767);
  begin
    l_buffer := p_string || ' Doesnt it?';
    --
    return l_buffer;
    --
  end ; 
--
select doesnt_it(add_string('Yes, it')) as outVal
from dual
;

:

OUTVAL
--------
Yes, it works! Doesnt it?
+3

Source: https://habr.com/ru/post/1653279/


All Articles