在插入语句Oracle中使用函数
Use function in insert Statement Oracle
我有这种类型的功能:
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;
我需要将这些数据插入 table 例如:
insert into my_table values(mfi_cust_details(myacid),anotherFunction());
但是我的程序甚至没有编译错误:
not enough values
我想做的事情是否可行?
编辑
我的 table 定义
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));
您的函数被定义为返回一列 - 即字符串 "value, value, value" 等(或 "Null, null, null" 等)[ 尽管我看不到将所有空值插入 my_table 错误!]。插入失败,因为插入 table(没有指定的列列表)将默认插入所有列,按照 table 中定义的顺序,但由于您的函数返回一列,它失败了"not enough values".
看来您正试图在插入语句中制作多列 - 我不确定是否有办法做到这一点。
您可以将您的函数定义为 returns ROWTYPE 本质上代表 table:
中的一条记录
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;
但随后您需要在 PL/SQL 块(即 BEGIN/END )内执行实际的插入语句。
declare
my_table%ROWTYPE rec;
begin
rec := mfi_cust_details('id');
insert into my_table( cust_title_code, cust_id, address1, address_2, city_code, country_code, zip_code)
values ( rec.cust_title_code, rec.cust_id, rec.address1, rec.address_2, rec.city_code, rec.country_code, rec.zip_code );
-- don't forget to commit;
end;
我有这种类型的功能:
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;
我需要将这些数据插入 table 例如:
insert into my_table values(mfi_cust_details(myacid),anotherFunction());
但是我的程序甚至没有编译错误:
not enough values
我想做的事情是否可行?
编辑 我的 table 定义
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));
您的函数被定义为返回一列 - 即字符串 "value, value, value" 等(或 "Null, null, null" 等)[ 尽管我看不到将所有空值插入 my_table 错误!]。插入失败,因为插入 table(没有指定的列列表)将默认插入所有列,按照 table 中定义的顺序,但由于您的函数返回一列,它失败了"not enough values".
看来您正试图在插入语句中制作多列 - 我不确定是否有办法做到这一点。
您可以将您的函数定义为 returns ROWTYPE 本质上代表 table:
中的一条记录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;
但随后您需要在 PL/SQL 块(即 BEGIN/END )内执行实际的插入语句。
declare
my_table%ROWTYPE rec;
begin
rec := mfi_cust_details('id');
insert into my_table( cust_title_code, cust_id, address1, address_2, city_code, country_code, zip_code)
values ( rec.cust_title_code, rec.cust_id, rec.address1, rec.address_2, rec.city_code, rec.country_code, rec.zip_code );
-- don't forget to commit;
end;