简体   繁体   English

如何将MS SQL存储过程转换为Oracle

[英]How to convert MS SQL Stored Procedure to Oracle

I am stuck with this query. 我对此查询感到困惑。 I try to convert but, i new at Oracle 11 g. 我尝试转换,但是,我是Oracle 11 g的新用户。

This is the ms sql stored procedure to convert with Oracle 11 g. 这是要与Oracle 11 g转换的ms sql存储过程。

CREATE PROCEDURE [dbo].[GetCustomers_Pager]
       @SearchTerm VARCHAR(100) = ''
      ,@PageIndex INT = 1
      ,@PageSize INT = 10
      ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY [CustomerID] ASC
      )AS RowNumber
      ,[CustomerID]
      ,[CompanyName]
      ,[ContactName]
      ,[City]
      INTO #Results
      FROM [Customers]
      WHERE [ContactName] LIKE @SearchTerm + '%' OR @SearchTerm = ''
      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
END

I find this ms sql stored procedure to this link 我找到此链接的此ms sql存储过程

This is my fail oracle stored procedure. 这是我失败的oracle存储过程。 I'm stuck with INTO syntax. 我坚持使用INTO语法。

CREATE OR REPLACE
PROCEDURE GetCustomers_Pager
( SearchTerm IN VARCHAR2
, PageIndex IN NUMBER DEFAULT 1
, PageSize IN NUMBER DEFAULT 10
, RecordCount OUT NUMBER
) AS
BEGIN  
  SELECT  
          ROW_NUMBER() OVER( ORDER BY CustomerID ASC )AS RowNumber,
          ,CustomerID
          ,CompanyName
          ,ContactName
          ,City
  INTO Results
  FROM Customers
  WHERE ( Customers LIKE SearchTerm + '%' OR ContactName LIKE SearchTerm + '%') OR SearchTerm = '';
  SELECT RecordCount = COUNT(*)
  FROM Results;
  SELECT * FROM Results
  WHERE RowNumber BETWEEN(PageIndex -1) * PageSize + 1 AND(((PageIndex -1) * PageSize + 1) + PageSize) - 1;
  DROP TABLE Results;

END GetCustomers_Pager;

Can anyone please help me? 谁能帮帮我吗?

You can use only simple type variable in the SELECT INTO clause, for selecting into array use SELECT BULK COLLECT INTO . 您只能在SELECT INTO子句中使用简单类型变量,使用SELECT BULK COLLECT INTO可以选择进入数组。 However you have to declare the variable you are selecting into and its type must match. 但是,您必须声明要选择的变量,并且其类型必须匹配。

If you want to select the count only, use this: 如果只想选择计数,请使用以下命令:

SELECT COUNT(*) INTO RecordCount
FROM (SELECT ROW_NUMBER() OVER( ORDER BY CustomerID ASC )AS RowNumber,
             CustomerID,
             CompanyName,
             ContactName,
             City
     FROM Customers
     WHERE ( Customers LIKE SearchTerm + '%' OR ContactName LIKE SearchTerm + '%') OR SearchTerm = '') s
WHERE RowNumber BETWEEN (PageIndex-1)*PageSize + 1 AND PageIndex*PageSize;

If you want to return the records, you have to define the array type, add the output parameter Records and use SELECT * BULK COLLECT INTO Records and then RecordCount := Records.COUNT; 如果要返回记录,则必须定义数组类型,添加输出参数Records并使用SELECT * BULK COLLECT INTO Records ,然后使用RecordCount := Records.COUNT; .

But the preferred way to return data is a REF CURSOR output parameter, see http://oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets.php . 但是返回数据的首选方法是REF CURSOR输出参数,请参见http://oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets.php

Oracle procedure code with cursor as output parameter: 使用游标作为输出参数的Oracle过程代码:

CREATE OR REPLACE PROCEDURE GetCustomers_Pager (
  SearchTerm IN VARCHAR2, PageIndex IN NUMBER DEFAULT 1, PageSize IN NUMBER DEFAULT 10, 
  RecordCount OUT NUMBER, Records out sys_refcursor ) AS

begin
  -- count all matching rows
  select count(1) into RecordCount from Customers
    where ContactName like SearchTerm||'%';

  -- get all matching rows into cursor "Records"
  -- and filter them including PageIndex and PageSize parameters
  open Records for 
  select rn, customerid, companyName, contactName, City 
    from (
      select row_number() over (order by customerID) rn,
          customerid, companyName, contactName, City
        from customers where ContactName like SearchTerm||'%' )
    where rn between (PageIndex-1) * PageSize
      and (((PageIndex-1) * PageSize + 1) + PageSize) - 1;
end GetCustomers_Pager;

Example C# code calling this procedure and filling DataGridView: 示例C#代码调用此过程并填充DataGridView:

OracleCommand cmd = new OracleCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "getcustomers_pager";
cmd.Connection = CONNECTION.oconn;
cmd.Parameters.Add(new OracleParameter("SearchTerm", OracleType.VarChar, 3)).Value = "Mar";
cmd.Parameters.Add(new OracleParameter("PageIndex", OracleType.Number)).Value = 1;
cmd.Parameters.Add(new OracleParameter("PageSize", OracleType.Number)).Value = 5;
OracleParameter p = new OracleParameter("RecordCount", OracleType.Number);
p.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p);
p = new OracleParameter("Records", OracleType.Cursor);
p.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p);

OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);

dgv.DataSource = ds.Tables[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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