postgresql - Postgresql 从复合类型数组和一个附加列中多插入
问题描述
我有的:
CREATE TYPE Item AS (
a bigint,
b bigint
);
CREATE TABLE items (
id bigint NOT NULL,
a bigint NOT NULL,
b bigint NOT NULL
);
CREATE OR REPLACE FUNCTION items_insert(
_id bigint,
_items Item[]
) RETURNS void AS
...
如何通过一个多插入查询将多列行插入到 _items 中具有相同 _id 的表项?
我正在使用 Postgresql-9.2
解决方案
我认为您的意思是“插入多行”而不是列。
假设这是正确的,我认为您正在寻找这样的功能:
create or replace function items_insert(_id bigint, _items item[])
returns void
as
$$
insert into items (id, a, b)
select _id, it.*
from unnest(_items) as it(a,b);
$$
language sql;
推荐阅读
- java - 如何修复 Spring Tool Suit 4 中的 Port 8080 错误?
- arrays - 快速访问 JSON 响应数组中的数组?
- c++ - 如何修复“没有这样的文件或目录”库错误?我正在使用 Mingw-w64 编译器和 Netbeans IDE
- django - 在 Django 中创建了自定义权限并为组和用户分配了权限,但 @permission_required 失败
- typescript - Nuxt.js组件中如何使用Vuex获取API?
- python - 我的碰撞函数中的for循环永远不会命中?
- javascript - React js组件在滚动事件中重新渲染两次
- windows - Nagios 插件 check_logfiles 用户和实施者在 windows 上的使用情况
- linux - Bash for 循环从多个目录中删除不带后缀的文件
- audio - 如何使 ffmpeg 的视频和音频持续时间相同?