gpt4 book ai didi

function - 如何在 PostgreSQL 函数的插入查询中插入参数值?

转载 作者:行者123 更新时间:2023-11-29 13:03:50 24 4
gpt4 key购买 nike

我在 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

请帮我指出问题

最佳答案

而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;

关于function - 如何在 PostgreSQL 函数的插入查询中插入参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com