在bytea列中插入字符串



我想使用concat函数或||操作符将文本数据插入Postgresbytea列。我得到一个错误

column "name" is of type bytea but expression is of type text
create table test(
name bytea
);
insert into test(name) values(concat('abac-' , 'test123'));
insert into test(name) values('aa' || 'bb');

我在一个存储过程中执行插入操作。如果想添加像

这样的参数
(concat('abac-' , row.name , 'test123'));

我该怎么做?

在连接值后执行类型强制转换:

INSERT INTO test (name)
VALUES (CAST('abac-' || row.name || 'test123' AS bytea));

注意:||concat的区别在于它们处理null的方式。

您需要将两个字符串强制转换为bytea,例如:

insert into test(name) values('aa'::bytea || 'bb'::bytea);

最新更新