将T-SQL存储过程转换为Oracle

时间:2018-01-08 14:02:03

标签: oracle tsql stored-procedures

我想将下面的T-SQL存储过程转换为Oracle存储过程吗?

CREATE PROCEDURE myProc
(
    @FirstName nvarchar(20),
    @LastName nvarchar(20)
) AS
BEGIN
    SELECT p.PersonId,
           p.Address,
           p.FirstName,
           p.LastName
      FROM People p
     WHERE p.FirstName = @FirstName
       AND p.LastName = @LastName
END
GO

1 个答案:

答案 0 :(得分:1)

您可以在oracle中将其转换为如下所示的过程:

SQL> set serveroutput on;
SQL> CREATE OR REPLACE PROCEDURE myProc
(
    i_FirstName varchar2(20), -- alternatively use People.FirstName%type instead of varchar2(20)
    i_LastName varchar(20),
    o_PersonId out People.PersonId%type,
    o_Address  out People.Address%type,
) AS
BEGIN
  for c in
   (
    SELECT p.PersonId, p.Address
      FROM People p
     WHERE p.FirstName = i_FirstName
       AND p.LastName = i_LastName
    )
   loop
     o_PersonId := c.PersonId;
     o_Address  := c.Address;
     dbms_output.put_line(' Person id : '||o_PersonId||' - Address : '||o_Address);
   end loop;
END;

SQL> var fname varchar2;
SQL> var lname varchar2;
SQL> exec myProc(:fname,:lname);
SQL> /

当@a_horse_with_no_name警告时,上面的一个适用于单行。您也可以使用以下工作来处理多行:

in
  • 您无需在in类型参数之前添加out关键字,但out类型参数需要NVARCHAR个关键字。

  • 如果使用Unicode字符集创建Oracle数据库,则应将VARCHAR2迁移到Oracle中的OR REPLACE

  • object already exists部分代表重复编译存储过程。如果省略,则会引发错误,告诉您Route::domain('{account}.myapp.com')->group(function () { Route::get('user/{id}', function ($account, $id) { // }); });