【发布时间】:2012-07-21 00:19:33
【问题描述】:
我有一个用 PostgreSQL 编写的函数,用于遍历一个大表并将大量值插入到另一个表中。输出很好,显然插入了很多行,但实际上没有值被插入到目标表(我的代码中的“资源”表)中。
我尝试将插入语句放入事务中,但无济于事。我是否缺少某种模糊的访问或权限设置?我在网上找到了几个像我一样这样做的例子,所以我在这个上拉了一点头发......
这是我的功能:
DECLARE
datatype_property record;
property record;
new_resource_id bigint;
BEGIN
RAISE NOTICE 'Starting...';
FOR datatype_property IN
SELECT * FROM datatype_properties
LOOP
RAISE NOTICE 'Trying to insert';
if not exists(select * from resources where uri = datatype_property.subject_resource) then
SELECT INTO new_resource_id NEXTVAL('resources_id_seq');
INSERT INTO resources (id, uri) VALUES(
new_resource_id,
datatype_property.subject_resource
);
RAISE NOTICE 'Inserted % with id %',datatype_property.subject_resource, new_resource_id;
end if;
END LOOP;
FOR property IN
SELECT * FROM properties
LOOP
if not exists(select * from resources where uri = property.source_uri) then
SELECT INTO new_resource_id NEXTVAL('resources_id_seq');
INSERT INTO resources (id, uri) VALUES(
new_resource_id,
resource.source_uri
) ;
RAISE NOTICE 'Inserted % with id %',resource.source_uri, new_resource_id;
end if;
if not exists(select * from resources where uri = property.destination_uri) then
SELECT INTO new_resource_id NEXTVAL('resources_id_seq');
INSERT INTO resources (id, uri) VALUES(
new_resource_id,
resource.source_uri
) ;
RAISE NOTICE 'Inserted % with id %',resource.source_uri, new_resource_id;
end if;
END LOOP;
RETURN;
结束;
编辑:我已经使用以下链接中的说明激活了 plpgsql 语言:
http://wiki.postgresql.org/wiki/CREATE_OR_REPLACE_LANGUAGE
编辑2:
这段代码:
DECLARE
datatype_property record;
property record;
new_resource_id bigint;
BEGIN
insert into resources (id, uri) values ('3', 'www.google.com');
END
也不行:O
【问题讨论】:
-
您检查了服务器日志文件吗?也许您的内存不足,或者您达到了可配置的限制(表大小、记录数等)
-
谢谢,我看看。
-
好吧,我检查了 postgres.log 文件,内容只是我在 pgadmin III 查询浏览器中可以看到的输出。
-
它看起来像未提交的事务。某些环境禁用自动提交 - 您必须明确提交。
标签: sql function postgresql stored-procedures insert