简体   繁体   中英

Need help in creating function table in PL/SQL

Need help here, I've created a function table

SELECT *
FROM TABLE (getsubs_api_v2(639377700080));

but using this script I can only output one row of data, so I'm trying to to use subquery inside TABLE() but it returns

ORA-01427: single-row subquery returns more than one row
01427. 00000 -  "single-row subquery returns more than one row"
*Cause:   
*Action:

My experiment query below:

SELECT * FROM TABLE   (SELECT getsubs_api_v2(SUBSTR(name, 2,
LENGTH(name)-1))   FROM pin.service_alias_list_t@brm_prod   WHERE
rec_id                         = 1   AND SUBSTR(name, 2,
LENGTH(name)-1) IN ('639377700080', '639373000273', '639373700013',
'639373700020', '639373700038')   );

Can you help me enhance this script?

In case you need the function getsubs_api_v2, please see below.

CREATE OR REPLACE FUNCTION getsubs_api_v2(mobtel number) RETURN t_tf_tab PIPELINED AS
  l_tab  t_tf_tab := t_tf_tab();
    soap_request  VARCHAR2(30000);
    soap_respond  CLOB;
    http_req      utl_http.req;
    http_resp     utl_http.resp;
    resp          XMLType;
    soap_err      exception;
    v_code        VARCHAR2(200);
    v_msg         VARCHAR2(1800);
    v_len number;
    v_txt Varchar2(32767);
  BEGIN
    --UTL_HTTP.SET_PROXY(p_proxy);
    -- Define the SOAP request according the the definition of the web service being called
    soap_request:= '<?xml version = "1.0" encoding = "UTF-8"?>'||
                   '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'||
                   '    <soap:Body xmlns:ns1="http://xmlns.oracle.com/SBLMVNE/MVNERetrieveSubscriberDetail/MVNERetrieveSubscriberDetailProcess">'||
                   '        <ns1:retrieveSubscriberDetailRequest>'||
                   '            <ns1:header>'||
                   '                <ns1:InterfaceName></ns1:InterfaceName>'||
                   '                <ns1:InterfaceId></ns1:InterfaceId>'||
                   '                <ns1:CorrelationId></ns1:CorrelationId>'||
                   '            </ns1:header>'||
                   '            <ns1:mvno></ns1:mvno>'||
                   '            <ns1:searchCriteriaType>MSISDN</ns1:searchCriteriaType>'||
                   '            <ns1:searchCriteriaValue>'||mobtel||'</ns1:searchCriteriaValue>'||
                   '        </ns1:retrieveSubscriberDetailRequest>'||
                   '    </soap:Body>'||
                   '</soap:Envelope>';
    http_req:= utl_http.begin_request
              ( 'http://192.168.0.1:8001/soa-infra/services/Dash/MVNERetrieveSubscriberDetail!1.0*soa_7e28c041-0e10-4cd0-b956-5ba9f6d5e56d/mvneretrievesubscriberdetailprocess_client_ep?wsdl'
              , 'POST'
              , 'HTTP/1.1'
              );
    utl_http.set_header(http_req, 'Content-Type', 'text/xml');
    utl_http.set_header(http_req, 'Content-Length', length(soap_request));
    utl_http.set_header(http_req, 'Download', ''); -- header requirements of particular web service
    utl_http.write_text(http_req, soap_request);
    http_resp:= utl_http.get_response(http_req);
    utl_http.get_header_by_name(http_resp, 'Content-Length', v_len, 1); -- Obtain the length of the response
    FOR i in 1..CEIL(v_len/32767) -- obtain response in 32K blocks just in case it is greater than 32K
    LOOP
        utl_http.read_text(http_resp, v_txt, case when i < CEIL(v_len/32767) then 32767 else mod(v_len,32767) end);
        soap_respond := soap_respond || v_txt; -- build up CLOB
    END LOOP;
    utl_http.end_response(http_resp);
    resp:= XMLType.createXML(soap_respond); -- Convert CLOB to XMLTYPE

    for y in (
    with test_mike as (select resp xml_data from dual)
    select x.*, x1.*, x2.*, x3.* from test_mike t,
  xmltable(xmlnamespaces('http://xmlns.oracle.com/SBLMVNE/MVNERetrieveSubscriberDetail/MVNERetrieveSubscriberDetailProcess' as "ns3",
  'http://schemas.xmlsoap.org/soap/envelope/' as "ns1", 'http://www.w3.org/2005/08/addressing' as "ns2"),
          'ns1:Envelope/ns1:Body/ns3:retrieveSubscriberDetailResponse/ns3:subscribers/ns3:Subscriber'
          passing t.xml_data
          columns subs_id number path 'ns3:subscriberId',
                  service_num number path 'ns3:serviceNo1',
                  duo_num number path 'ns3:serviceNo2',
                  iccid varchar2(30) path 'ns3:serviceNo4',
                  creation_date varchar2(30) path 'ns3:creationDt',
                  subs_status xmltype path 'ns3:subscriberStatus',
                  cos xmltype path 'ns3:cos',
                  wallets xmltype path 'ns3:wallets/ns3:wallet') x,
  xmltable(xmlnamespaces('http://xmlns.oracle.com/SBLMVNE/MVNERetrieveSubscriberDetail/MVNERetrieveSubscriberDetailProcess' as "ns1"),
          'ns1:subscriberStatus'
          passing x.subs_status
          columns mvne_status number path 'ns1:Status',
                  status_date varchar2(30) path 'ns1:statusDt',
                  expiration_date varchar2(30) path 'ns1:statusExpiryDt') x1,
  xmltable(xmlnamespaces('http://xmlns.oracle.com/SBLMVNE/MVNERetrieveSubscriberDetail/MVNERetrieveSubscriberDetailProcess' as "ns1"),
          'ns1:cos'
          passing x.cos
          columns cos_id number path 'ns1:Id',
                  cos_name varchar2(10) path 'ns1:Name') x2,
  xmltable(xmlnamespaces('http://xmlns.oracle.com/SBLMVNE/MVNERetrieveSubscriberDetail/MVNERetrieveSubscriberDetailProcess' as "ns1"),
          'ns1:wallet'
          passing x.wallets
          columns wallet_id number path 'ns1:Id',
                  wallet_name varchar2(20) path 'ns1:Name',
                  wallet_type varchar2(20) path 'ns1:Type',
                  wallet_balance number path 'ns1:Balance',
                  wallet_currency varchar2(10) path 'ns1:currency',
                  wallet_expdt varchar2(30) path 'ns1:expiryDt') x3) loop
    --l_tab.extend;
    pipe row(t_tf_row(y.subs_id, y.service_num, y.duo_num,y.iccid,y.creation_date,y.mvne_status,y.status_date,y.expiration_date,y.cos_id,y.cos_name,y.wallet_id,y.wallet_name,y.wallet_type,y.wallet_balance,y.wallet_currency,y.wallet_expdt));
    end loop;
    return;
  END;

Okay, so I've revised my query

SELECT *
FROM TABLE
  (getsubs_api_v2(cursor(select SUBSTR(name, 2, LENGTH(name)-1)
  FROM pin.service_alias_list_t@brm_prod
  WHERE rec_id                         = 1
  AND SUBSTR(name, 2, LENGTH(name)-1) IN ('639373000273','639377700080')
  )));

but this time, I get an error:

ORA-06553: PLS-306: wrong number or types of arguments in call to 'GETSUBS_API_V2'
06553. 00000 -  "PLS-%s: %s"
*Cause:    
*Action:
Error at Line: 3 Column: 4

Remove second SELECT from this line:

SELECT * FROM TABLE   (SELECT getsubs_api_v2(SUBSTR(name, 2,

Just:

SELECT * FROM TABLE   ( getsubs_api_v2(SUBSTR(name, 2,

See an example in the documentation how a pipelined function should be called:
https://docs.oracle.com/cd/B28359_01/appdev.111/b28425/pipe_paral_tbl.htm#CIHBGDJD

Example 13-10 How to Pipeline Function Results from One Function to Another

SELECT * FROM TABLE( f(CURSOR(SELECT * FROM TABLE(g()))) ) ;

As you see in this example, there is no any SELECT clause between TABLE(
and a pipelined function name f( .

The TABLE() syntax for a table collection expression needs to be passed a collection expression; that can be a subquery but the documentation says :

The collection_expression can be a subquery, a column, a function, or a collection constructor. Regardless of its form, it must return a collection value—that is, a value whose type is nested table or varray.

You are using a subquery which returns multiple pipelined function results, but not as a collection. You could potentially use multiset to get all the function results into a collection but it would be long-winded and isn't necessary.

You need to cross-join your real table with a table collection expression that uses the values from the real table:

SELECT t.*
FROM pin.service_alias_list_t@brm_prod salt
CROSS JOIN TABLE (getsubs_api_v2(SUBSTR(salt.name, 2, LENGTH(salt.name )-1))) t
WHERE salt.rec_id = 1
AND SUBSTR(salt.name, 2, LENGTH(salt.name)-1) IN ('639373000273','639377700080');

I've given your real table an alias of salt and the table collection expression an alias of t so you can distinguish between them. Your function is now called for each row in your real table.

Passing string into the function will cause implicit conversion, which isn't ideal; an explicit to_number() would be better, as long as Oracle doesn't try to evaluate that for substrings that are no in your IN list and which can't be converted; which it shouldn't do.

Quick demo with a dummy function:

create function pipe_demo(p_string varchar2, p_quantity number)
return sys.odcivarchar2list pipelined as
begin
  for i in 1..p_quantity loop
    pipe row (p_string);
  end loop;
  return;
end;
/

And table:

create table demo_table(string varchar2(20), quantity number);
insert into demo_table (string, quantity) values ('First string', 3);
insert into demo_table (string, quantity) values ('Second string', 2);
insert into demo_table (string, quantity) values ('Third string', 4);

And equivalent query:

select t.column_value
from demo_table dt
cross join table(pipe_demo(dt.string, dt.quantity)) t;

COLUMN_VALUE       
--------------------
First string        
First string        
First string        
Second string       
Second string       
Third string        
Third string        
Third string        
Third string        

 9 rows selected 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM