【发布时间】:2014-07-07 21:08:23
【问题描述】:
我查看了this 类似的问题,但我认为我的问题可能有所不同。如果我正确理解他们的问题,则问题是由上游语法错误引起的。
在我的例子中,语法错误非常接近程序的开头,它给了我一个可能出错的狭窄窗口,但在我看来一切正常。
代码:
DO $$
DECLARE topic_cursor CURSOR FOR SELECT * FROM "socialMediaModel_topic" WHERE "active_search"=True;
BEGIN
OPEN topic_cursor;
FETCH FIRST FROM topic_cursor;
LOOP
SELECT "topic" FROM topic_cursor AS "c_topic";
SELECT "topic_id" FROM topic_cursor AS "c_id";
SELECT "active_search" FROM topic_cursor AS "c_active";
INSERT INTO "socialMediaModel_datacollection" ("name", "active")
VALUES (c_topic, c_active);
INSERT INTO "socialMediaModel_datacollectiontopic" ("data_collection_id_id", "topic_id_id")
VALUES ((SELECT "data_collection_id" FROM "DataCollection" where name=c_topic), c_id);
FETCH NEXT FROM topic_cursor;
END LOOP;
CLOSE topic_cursor;
UPDATE "socialMediaModel_topic" SET "active_search" = False WHERE "active_search"=True;
COMMIT;
END$$;
错误:
ERROR: syntax error at or near ";"
LINE 9: FETCH FIRST FROM topic_cursor;
^
********** Error **********
ERROR: syntax error at or near ";"
SQL state: 42601
Character: 247
在编写此脚本时,我几乎完全遵循了这些资源:
数据库:PostgreSQL 9.1
编辑:pgAdmin III 查询工具
如果我遗漏了一些非常明显的东西,我会提前道歉。我整天盯着这个剧本,所以我的大脑可能有点乱。
【问题讨论】:
-
也许“从 yyy INTO 变量名中获取 xxx”?顺便说一句:我认为您需要光标来执行此任务。
-
您的前三个
select语句应为fetch,如您添加的第一个链接的示例所示。您不能在光标上使用select。并且使用for some_rec in select ... loop编写的代码要少得多(请参阅页面末尾第一个链接中的示例) -
data_collection_id_id,topic_id_id?错别字?
标签: sql postgresql cursor plpgsql window-functions