需要帮助在PL / SQL中创建函数表

时间:2016-02-20 05:48:30

标签: sql oracle function

在这里需要帮助,我已经创建了一个功能表

SELECT *
FROM TABLE (getsubs_api_v2(639377700080));

但是使用这个脚本我只能输出一行数据,因此我试图在TABLE()内使用子查询,但它返回

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

我的实验查询如下:

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')   );

你能帮助我改进这个脚本吗?

如果您需要函数getsubs_api_v2,请参阅下文。

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;

好的,所以我修改了我的查询

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')
  )));

但是这一次,我收到了一个错误:

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

2 个答案:

答案 0 :(得分:0)

从此行中删除第二个SELECT:

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

只需:

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

请参阅文档中的示例,了解如何调用流水线函数:
https://docs.oracle.com/cd/B28359_01/appdev.111/b28425/pipe_paral_tbl.htm#CIHBGDJD

  

示例13-10如何将函数结果从一个函数转换为   另一

     

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

正如您在此示例中所看到的,TABLE(
和管道函数名f(之间没有任何SELECT子句。

答案 1 :(得分:0)

table collection expressionTABLE()语法需要传递给集合表达式;可以是子查询,但the documentation says

  

collection_expression可以是子查询,列,函数或集合构造函数。无论其形式如何,它都必须返回一个集合值 - 即类型为嵌套表或varray的值。

您正在使用子查询返回多个流水线函数结果,但不是作为集合。您可以使用multiset将所有功能结果集中到一个集合中,但这样做是冗长的,并且没有必要。

您需要将实际表与表集合表达式交叉连接,该表达式使用真实表中的值:

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');

我已经为您的真实表格提供了salt的别名,并且表格集合表达式为t的别名,因此您可以区分它们。现在为实际表格中的每一行调用您的函数。

将字符串传递给函数将导致隐式转换,这不是理想的;明确的to_number()会更好,只要Oracle没有尝试评估那些在IN列表中不存在且无法转换的子字符串;不应该这样做。

带虚拟功能的快速演示:

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;
/

表:

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);

等效查询:

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