使用插入语句Oracle

时间:2015-07-13 10:31:49

标签: oracle function sql-insert

我有这种类型的功能:

 FUNCTION mfi_cust_details (vacid VARCHAR2)
      RETURN VARCHAR2
   IS
      vcustdetails   VARCHAR2 (300);
   BEGIN
      BEGIN
         SELECT    a.cust_title_code
                || ','
                || a.cust_id
                || ','
                || b.address_line1
                || ','
                || b.address_line2
                || ','
                || mfi_citycountry (b.country, b.city)
                || ','
                || b.zip
           INTO vcustdetails
           FROM tbaadm.cmg a, crmuser.address b
          WHERE TRIM (a.cif_id) = TRIM (b.orgkey)
            AND UPPER (b.addresscategory) IN ('MAILING', 'REGISTERED')
            AND cust_id IN (SELECT cust_id
                            FROM tbaadm.gam
                            WHERE acid = vacid);
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN
            vcustdetails :=
                    NULL
               || ','
               || NULL
               || ','
               || NULL
               || ','
               || NULL
               || ','
               || NULL
               || ','
               || NULL
               || ','
               || NULL;
      END;

      RETURN vcustdetails;
   END mfi_cust_details;

我需要将这些数据插入表中,例如:

insert into my_table values(mfi_cust_details(myacid),anotherFunction());

但我的程序甚至没有编译错误:

 not enough values

是我想做的任何事情吗?

修改 我的表定义

    create table my_table cust_title_code varchar2(10),
cust_id varchar2(10),
address1 varchar2(10),
address_2 varchar2(10),
city_code varchar2(5),
country_code varchar2(5),
zip_code varchar2(10));

1 个答案:

答案 0 :(得分:3)

你的函数定义为返回一列 - 这是字符串“value,value,value”等(或“Null,null,null”等)[虽然我看不到使用插入所有空值错误的my_table!]。插入失败,因为插入表(没有指定的列列表)将默认按照表中定义的顺序插入所有列,但由于您的函数返回一列,因此失败并显示“没有足够的值”。

看来你正试图在insert语句中创建多个列 - 我不确定是否有办法做到这一点。

可以将您的函数定义为返回ROWTYPE的函数,该ROWTYPE实际上代表该表中的一条记录:

this.props

但是你需要在PL / SQL块中进行实际的insert语句(即BEGIN / END)。

FUNCTION mfi_cust_details(vacid VARCHAR2) return my_table%ROWTYPE as 
  my_table%ROWTYPE vcustdetails;
BEGIN
  SELECT a.cust_title_code,
    a.cust_id,
    b.address_line1,
    b.address_line2,
    mfi_citycountry (b.country, b.city),
    b.zip
  INTO vcustdetails 
  FROM tbaadm.cmg a, crmuser.address b
            WHERE TRIM (a.cif_id) = TRIM (b.orgkey)
              AND UPPER (b.addresscategory) IN ('MAILING', 'REGISTERED')
              AND cust_id IN (SELECT cust_id
                              FROM tbaadm.gam
                              WHERE acid = vacid);  

  RETURN vcustdetails;
END;