我在PostgreSQL中用一个参数编写了一个函数,我想在数据库表中插入参数值。脚本执行正常,但是当我调用该函数时,我收到一条错误消息:
CREATE OR REPLACE FUNCTION sp_load_purchase_order(x int)
RETURNS void AS
$$
declare var_val char;
begin
var_val='p'+i;
insert into purchase_order(
create_uid,
create_date,
write_date,
write_uid,
journal_id,
date_order,
partner_id,
amount_untaxed,
location_id,
company_id,
amount_tax,
state,
pricelist_id,
warehouse_id,
payment_term_id,
amount_total,
name,
invoice_method,
shipped,
minimum_planned_date
)
values(1,now(),now(),1,13,now(),17,1.00,12,1,0.00,'draft',2,1,3,1.00
,var_val,'order','f' ,now()
);
end;
$$
LANGUAGE 'plpgsql';
错误讯息:
ERROR: column "i" does not exist
LINE 1: SELECT 'p'+i
^
QUERY: SELECT 'p'+i
CONTEXT: PL/pgSQL function sp_load_purchase_order(integer) line 5 at assignment
********** Error **********
ERROR: column "i" does not exist
SQL state: 42703
Context: PL/pgSQL function sp_load_purchase_order(integer) line 5 at assignment
请帮我指出问题。
答案 0 :(得分:2)
plpgsql中的赋值运算符为:=
:
The forgotten assignment operator "=" and the commonplace ":="
不 引用语言名称plpgsql
!
并且您不需要为此声明变量。连接可以在INSERT
语句中进行,该语句更便宜。
CREATE OR REPLACE FUNCTION sp_load_purchase_order(x int)
RETURNS void AS
$func$
begin
insert into purchase_order(create_uid, ..., name, ...)
values(1, ..., 'p' || x, ...);
end
$func$ LANGUAGE plpgsql;
答案 1 :(得分:0)
您的函数参数名为x
,但您的函数体引用了一个名为i
的未声明变量......
答案 2 :(得分:0)
我在我的剧本中犯了两个错误的错误...一个是未声明的'i'变量,另一个是我的连接尝试,我使用'+'符号...这里是工作代码::: :
CREATE OR REPLACE FUNCTION sp_load_purchase_order(x int)
RETURNS void AS
$$
declare var_val char(10);
begin
var_val='p'||x;
insert into purchase_order(
create_uid,
create_date,
write_date,
write_uid,
journal_id,
date_order,
partner_id,
amount_untaxed,
location_id,
company_id,
amount_tax,
state,
pricelist_id,
warehouse_id,
payment_term_id,
amount_total,
name,
invoice_method,
shipped,
minimum_planned_date
)
values(
1,now(),now(),1,13,now(),17,1.00,12,1,0.00,'draft',2,1,3,1.00,var_val,'order','f' ,now()
);
end;
$$
LANGUAGE 'plpgsql';