Sql server Oracle PL SQL过程,相当于Sequel/Sybase中的T SQL

Sql server Oracle PL SQL过程,相当于Sequel/Sybase中的T SQL,sql-server,oracle,stored-procedures,plsql,sybase,Sql Server,Oracle,Stored Procedures,Plsql,Sybase,我是甲骨文的新手。来自SQL/Sybase背景,努力创建一个只需从表中输出一组字段的过程。 我希望在Oracle中创建一个与SQL/Sybase等效的存储过程,如下所示: CREATE PROCEDURE ListTransactions @StartDate datetime, @EndDate datetime AS DECLARE @ReportDate datetime SET @ReportDate = CONVERT(datetime,CONVERT(VARCHAR,GETDATE(

我是甲骨文的新手。来自SQL/Sybase背景,努力创建一个只需从表中输出一组字段的过程。 我希望在Oracle中创建一个与SQL/Sybase等效的存储过程,如下所示:

CREATE PROCEDURE ListTransactions @StartDate datetime, @EndDate datetime AS
DECLARE @ReportDate datetime
SET @ReportDate = CONVERT(datetime,CONVERT(VARCHAR,GETDATE(), 103), 103)
SELECT
   @ReportDate,
   trn.TransactionDate,
   cp.CounterPartyName,
   cu.Currency_Name,
   trn.Amount,
   trn.PostedBy,
   trn.Comments
FROM
   Transactions trn
   LEFT JOIN CounterParties cp ON trn.CounterParty_Id = cp.CounterParty_Id
   LEFT JOIN Currency cu ON trn.Currency_Id = cu.Currency_Id
WHERE
   trn.TransactionDate Between @StartDate AND @EndDate

以下操作应与您想要的类似:

CREATE OR REPLACE FUNCTION LIST_TRANSACTIONS(pStartDate IN DATE,
                                             pEndDate   IN DATE)
  RETURN SYS_REFCURSOR IS
DECLARE
  csr SYS_REFCURSOR;
BEGIN
  OPEN csr FOR
    SELECT TRUNC(SYSDATE),
           trn.TransactionDate,
           cp.CounterPartyName,
           cu.Currency_Name,
           trn.Amount,
           trn.PostedBy,
           trn.Comments
      FROM Transactions trn
      LEFT OUTER JOIN CounterParties cp
        ON trn.CounterParty_Id = cp.CounterParty_Id
      LEFT OUTER JOIN Currency cu
        ON trn.Currency_Id = cu.Currency_Id
      WHERE trn.TransactionDate BETWEEN pStartDate
                                    AND pEndDate;
  RETURN csr;
END LIST_TRANSACTIONS;
我不明白你所说的“从表中输出一组字段”是什么意思。此函数将返回一个游标,调用方随后可以迭代该游标。例如,如果要将光标的结果转储到DBMS_输出,可以执行以下类似匿名块的操作:

DECLARE
  c                    SYS_REFCURSOR;
  dtDate               DATE;
  dtTransaction_date   DATE;
  strCounterPartyName  VARCHAR2(100);
  strCurrency_Name     VARCHAR2(100);
  nAmount              NUMBER;
  strPostedBy          VARCHAR2(100);
  strComments          VARCHAR2(2000);
BEGIN
  c := LIST_TRANSACTIONS(pStartDate => SYSDATE - INTERVAL '1' MONTH,
                         pEndDate   => SYSDATE);

  LOOP
    FETCH c
      INTO dtDate,
           dtTransaction_date,
           strCounterPartyName,
           strCurrency_Name,
           nAmount,
           strPostedBy,
           strComments;

    EXIT WHEN c%NOTFOUND;

    DBMS_OUTPUT.PUT_LINE('Date=' || dtDate || 
                         'Transaction date=' || dtTransaction_date ||
                         'Counter-party namme=' || strCounterPartyName ||
                         'Currency name=' || strCurrency_Name ||
                         'Amount=' || nAmount ||
                         'Posted by=' || strPostedBy ||
                         'Comments=' || strComments);
  END LOOP;

  CLOSE c;
END;
没有在动物身上测试-你会是第一个


分享和享受。

FYI,有趣的@ReportDate双转换是T-SQLs,相当于Oracle的TRUNC语句。它将时间部分设置为全零,即午夜。尽管我更喜欢
DateAdd(d,0,DateDiff(d,0,getdate())
,它对严格的算术函数做同样的事情,而不是文本格式化/解析。