SQLSERVER2005中的表值函数在ORACLE中怎么写啊?大侠们,帮帮忙.

解决方案 »

  1.   

    是不是这个意思?http://www.itpub.net/thread-1079605-1-1.html
      

  2.   

    可以尝试一下自定义type.例如:
    create or replace type test_typ as object
    (
      id  number,
      val varchar2(20)
    )

    create or replace type test_tbl_typ as table of test_typ
    /
      

  3.   

    谢谢大侠们!但是我想在函数里面用INSERT语句插入到临时表中并且返回临时表的数据?急.......
      

  4.   

    用自定义typeselect *
    from table(cast(B as A))create or replace type CTest as object
    (
    tid varchar2(6),
    tname varchar2(20)
    )
    /
    create or replace type tabCtest is table of cTest/
    create or replace function myTest return tabctest
    is
    Result tabctest := tabctest();
    begin
    Result := tabctest();
    Result.extend;
    Result(Result.count) := CTEST(NULL,NULL);
    Result(Result.count).tid := '1';
    Result(Result.count).tname := 'name1';Result.extend;
    Result(Result.count) := CTEST(NULL,NULL);
    Result(Result.count).tid := '2';
    Result(Result.count).tname := 'name2';return(Result);
    end myTest;
    /select *
    from table(cast(myTest as tabctest))
    能不能把Result代替一个临时表,进行insert,delete,update?求救,谢谢!
      

  5.   

    function是不能返回结果集的
    一般是返回单个值,不然你在select func() from table的时候,返回多个数据值,就不符合关系型数据库结构了.
      

  6.   

    但是SQLSEVER2005 是可以返回记录集的.
    ALTER FUNCTION [dbo].[getWBSTreeProj]  (@projectId bigint)
    returns @tree_table table(
        id bigint,parentid bigint,leafflag int,releval int,
        qty numeric(15,4),price numeric(15,4),
        code varchar(100),name varchar(200),unit bigint,taskindex bigint,
        projectid bigint,orgid bigint
    )
    BEGIN    --根据projectId找最层的WBS节点
        insert into @tree_table
        select a.id,a.parentid,1,0,a.qty,a.price,a.code,a.name,a.unit,a.taskindex,a.projectid,a.dutyorgid
        from t_pm_plan_wbs a
        where a.projectid = @projectId
          and a.id not in (select parentid from t_pm_plan_wbs where parentid is not null group by parentid)    --反推出全树
        declare @releval int
        set @releval = 0
        while @releval < 20 and @@rowcount > 0
        begin
          delete from @tree_table where id in ( select parentid from @tree_table a  where releval = @releval)      insert into @tree_table
          select a.id,a.parentid,0, @releval + 1,a.qty,
            (select sum(b.qty * b.price) / a.qty from @tree_table b where b.parentid = a.id  ) price,
            a.code,a.name,a.unit,a.taskindex,a.projectid,a.dutyorgid
          from t_pm_plan_wbs a
          where a.id in ( select parentid from @tree_table a  where releval = @releval)      set @releval = @releval + 1
        end    RETURN
    END