将表变量从SQL Server 2008 R2迁移到Oracle 11g

时间:2017-09-24 00:56:50

标签: plsql oracle11g sql-server-2008-r2

我正在尝试将此代码从SQL Server迁移到Oracle,但是我遇到了很多错误;请有人帮帮我吗?

SQL Server中的代码:

DECLARE @tblFacultad TABLE
( IdFacultad INT,
  NomFacultad VARCHAR(200)  
);

INSERT INTO @tblFacultad (IdFacultad, NomFacultad) 
    SELECT IdFacultad, NomFacultad 
    FROM FACULTAD_local

SELECT * FROM @tblFacultad

Oracle中的代码:

CREATE OR REPLACE TYPE objFacultad AS OBJECT
(
   IdFacultad NUMBER,
   NomFacultad varchar(255)
)

/
CREATE OR REPLACE TYPE tblFacultad is table of objFacultad;

/
SELECT IDFACULTAD,NOMFACULTAD BULK COLLECT INTO tblFacultad FROM FACULTAD;

当我在Oracle中运行代码时,我收到此错误:

  

ORA-03001:未实现的功能
  03001. 00000 - “未实现的功能”
  *原因:未实现此功能   *行动:无。
  错误enlaínea:11,columna:54

1 个答案:

答案 0 :(得分:1)

您无法在Oracle SQL中定义这样的表变量。您可以定义集合类型,然后可以将其用作表列类型或(更常见地)作为PL / SQL代码中的类型。例如:

create or replace type objfacultad as object
( idfacultad number
, nomfacultad varchar(255)
)
/

create or replace type tblfacultad is table of objfacultad;
/

declare
    demo_t tblfacultad;
begin
    select objfacultad(idfacultad,nomfacultad) bulk collect into demo_t
    from   ( select 1 as idfacultad, 'F1' as nomfacultad from dual
             union all
             select 2 as idfacultad, 'F2' as nomfacultad from dual ) facultad;

    dbms_output.put_line('Array contains ' || demo_t.count || ' elements:');

    for r in (
        select f.idfacultad, f.nomfacultad
        from   table(demo_t) f
    )
    loop
        dbms_output.put_line(r.idfacultad || ' ' || r.nomfacultad);
    end loop;
end;
/

输出:

Array contains 2 elements:
1 F1
2 F2

请注意,create type语句为SQL,而declare开头的部分为PL/SQL块。与Oracle合作时,我们必须意识到这种区别。无论我使用PL / SQL Developer,Toad,文本编辑器还是其他任何东西都没有区别。