如何使用oracle从sql查询创建过程/函数?

如何使用oracle从sql查询创建过程/函数?,oracle,function,stored-procedures,stored-functions,bindvalue,Oracle,Function,Stored Procedures,Stored Functions,Bindvalue,我有一个有趣的情况 这是我的密码 SELECT :dateSend, t.senderPostindex, t.recipientPostindex, d.shipment_days FROM TABLE t INNER JOIN shipment_days d on d.first_index = t.senderIndex and d.second_index = t.recipientPostindex WHERE t.senderPostindex = variablesenderPos

我有一个有趣的情况

这是我的密码

SELECT :dateSend, t.senderPostindex, t.recipientPostindex, d.shipment_days FROM TABLE t
INNER JOIN shipment_days d on d.first_index = t.senderIndex and d.second_index = t.recipientPostindex
WHERE t.senderPostindex = variablesenderPostindex AND t.recipientPostindex = variablerecipientPostindex
基本上,我需要像getShipmentDays(“01001”、“02031”、“2020/05/09 15:40:00”)这样的程序 它必须回报我

  • 发送日期:2020/05/09 15:40:00
  • 发件人邮件索引:01001
  • 收件人邮政索引:02031
  • 装运天数:5天

我试图创建这个函数,但老实说我很难理解它,有人能帮我吗?p、 c.dateSend是一个绑定变量,我没有它

一个选项是创建一个带有
OUT
参数的过程

由于您没有发布测试用例,所以我自己创建了一个

SQL> create table taby as
  2    select 1 senderindex, 1 recipientpostindex, 1 senderpostindex from dual;

Table created.

SQL> create table shipment_days as
  2    select 5 shipment_days, 1 first_Index, 1 second_index from dual;

Table created.
您的查询已转换为过程:

SQL> create or replace procedure getshipmentdays
  2    (par_date    in out date,
  3     par_varsend in     varchar2,
  4     par_varrec  in     varchar2,
  5     --
  6     par_sendix  out    taby.senderpostindex%type,
  7     par_recix   out    taby.recipientpostindex%type,
  8     par_days    out    shipment_days.shipment_days%type
  9    )
 10  as
 11  begin
 12    select par_date,
 13           t.senderpostindex,
 14           t.recipientpostindex,
 15           d.shipment_days
 16      into par_date,
 17           par_sendix,
 18           par_recix,
 19           par_days
 20      from taby t join shipment_days d on d.first_index = t.senderindex
 21                                      and d.second_index = t.recipientpostindex
 22      where t.senderpostindex = par_varsend
 23        and t.recipientpostindex = par_varrec;
 24  end;
 25  /

Procedure created.
测试:由于存在多个
OUT
参数,因此必须声明变量以接受其值

SQL> set serveroutput on;
SQL> declare
  2    l_date    date := date '2020-06-09';
  3    l_sendix  taby.senderpostindex%type;
  4    l_recix   taby.recipientpostindex%type;
  5    l_days    shipment_days.shipment_days%type;
  6  begin
  7    getshipmentdays(l_date, 1, 1, l_sendix, l_recix, l_days);
  8    dbms_output.put_line('date = ' || l_date     ||', '||
  9                         'senderPostindex = '    || l_sendix ||', '||
 10                         'recipientpostindex = ' || l_recix  ||', '||
 11                         'shipment_days = '      || l_days
 12                        );
 13  end;
 14  /
date = 09.06.20, senderPostindex = 1, recipientpostindex = 1, shipment_days = 5

PL/SQL procedure successfully completed.

SQL>

另一个选项是创建一个返回refcursor的函数:

SQL> create or replace function fgetshipmentdays
  2    (par_date    in  date,
  3     par_varsend in  varchar2,
  4     par_varrec  in  varchar2
  5    )
  6  return sys_refcursor
  7  as
  8    l_rc sys_refcursor;
  9  begin
 10    open l_rc for
 11    select par_date,
 12           t.senderpostindex,
 13           t.recipientpostindex,
 14           d.shipment_days
 15      from taby t join shipment_days d on d.first_index = t.senderindex
 16                                      and d.second_index = t.recipientpostindex
 17      where t.senderpostindex = par_varsend
 18        and t.recipientpostindex = par_varrec;
 19    return l_rc;
 20  end;
 21  /

Function created.

SQL> var rc refcursor
SQL> exec :rc := fgetshipmentdays(date '2020-06-09', 1, 1);

PL/SQL procedure successfully completed.

SQL> print rc

:B3      SENDERPOSTINDEX RECIPIENTPOSTINDEX SHIPMENT_DAYS
-------- --------------- ------------------ -------------
09.06.20               1                  1             5

SQL>

或者,如您所愿,使用表函数

首先创建类型:

SQL> create type t_sd_row as object
  2    (datum  date,
  3     sendix varchar2(10),
  4     recix  varchar2(10),
  5     days   number
  6    );
  7  /

Type created.

SQL> create type t_sd_tab as table of t_sd_row;
  2  /

Type created.
功能:

SQL> create or replace function getshipmentdays
  2    (par_date    in  date,
  3     par_varsend in  varchar2,
  4     par_varrec  in  varchar2
  5    )
  6  return t_sd_tab as
  7    l_tab t_sd_tab := t_sd_tab();
  8  begin
  9    select t_sd_row(par_date,
 10                    t.senderpostindex,
 11                    t.recipientpostindex,
 12                    d.shipment_days
 13                   )
 14      bulk collect into l_tab
 15      from taby t join shipment_days d on d.first_index = t.senderindex
 16                                      and d.second_index = t.recipientpostindex
 17      where t.senderpostindex = par_varsend
 18        and t.recipientpostindex = par_varrec;
 19    return l_tab;
 20  end;
 21  /

Function created.
测试:

SQL> select * from table(getshipmentdays(date '2020-06-08', 1, 1));

DATUM            SENDIX     RECIX            DAYS
---------------- ---------- ---------- ----------
08/06/2020 00:00 1          1                   5

SQL>

你能举例说明我如何使用select执行这个函数吗?我需要使用选择。。。类似于“从表中选择*(getshipmentdays(当前时间戳,'01001','65012'));”当然;我编辑了我的答案并添加了另一个例子。还有一个问题,如何将它与时间戳一起使用?我有一个错误。。我指的是时间戳而不是DATEWell,使用时间戳&传递时间戳而不是日期,例如从表中选择*(getshipmentdays(时间戳'2020-06-08 21:16:20.123',1,1))4 ORA-22905:无法访问非嵌套表项SQL3.sql 1 15中的行。。。