迭代ARRAY,声明临时表。 PostgreSQL的

时间:2015-12-12 16:06:55

标签: sql postgresql cursor postgresql-9.3

我传递了一个id列表,我需要遍历该列表并进行SELECTIONS。

CREATE TABLE IF NOT EXISTS unique_things_block 
(
    id SERIAL PRIMARY KEY,
    unique_thing_block_name VARCHAR(64)
);

CREATE TABLE IF NOT EXISTS unique_thing 
(
    id SERIAL PRIMARY KEY,
    unique_thing_name VARCHAR(64),
    unique_thing_block INTEGER references unique_things_block(id),
    unique_thing_description VARCHAR(256),
    is_boolean boolean NOT NULL
);

CREATE TABLE IF NOT EXISTS permutations 
(
    id SERIAL NOT NULL,
    unique_thing_id INTEGER references unique_thing(id),
    PRIMARY KEY(id, unique_thing_id)

);


INSERT INTO unique_thing 
VALUES(1, 'dgsg', 1, 'It means that blablabla...', FALSE);

INSERT INTO unique_thing 
VALUES(2, 'dfgdfg', 1, 'Too long text...', FALSE);

INSERT INTO unique_thing 
VALUES(3, 'sdf wdfs', 2, 'It means that blablabla...', FALSE);

INSERT INTO unique_thing 
VALUES(4, 'sdf fdg fdg', 2, 'Too long text...', FALSE);

INSERT INTO unique_thing 
VALUES(5, 'dfg dfg', 2, 'dfg Too long text...', FALSE);

INSERT INTO unique_thing 
VALUES(6, 'dfg dfg', 2, 'df Too long text...', FALSE);

INSERT INTO unique_thing 
VALUES(7, 'dfg', 3, 'How to make...', TRUE);

INSERT INTO unique_thing 
VALUES(8, 'dfg', 3, 'asdasdsd...', TRUE);

INSERT INTO permutations VALUES(1, 1);
INSERT INTO permutations VALUES(1, 3);
INSERT INTO permutations VALUES(1, 7);
INSERT INTO permutations VALUES(2, 1);
INSERT INTO permutations VALUES(2, 3);
INSERT INTO permutations VALUES(2, 8);
INSERT INTO permutations VALUES(3, 1);
INSERT INTO permutations VALUES(3, 4);
INSERT INTO permutations VALUES(3, 7);
INSERT INTO permutations VALUES(4, 1);
INSERT INTO permutations VALUES(4, 4);
INSERT INTO permutations VALUES(4, 8);
INSERT INTO permutations VALUES(5, 1);
INSERT INTO permutations VALUES(5, 5);
INSERT INTO permutations VALUES(5, 7);
INSERT INTO permutations VALUES(6, 1);
INSERT INTO permutations VALUES(6, 5);
INSERT INTO permutations VALUES(6, 8);
INSERT INTO permutations VALUES(7, 1);
INSERT INTO permutations VALUES(7, 6);
INSERT INTO permutations VALUES(7, 7);
INSERT INTO permutations VALUES(8, 1);
INSERT INTO permutations VALUES(8, 6);
INSERT INTO permutations VALUES(8, 8);
INSERT INTO permutations VALUES(9, 2);
INSERT INTO permutations VALUES(9, 3);
INSERT INTO permutations VALUES(9, 7);
INSERT INTO permutations VALUES(10, 2);
INSERT INTO permutations VALUES(10, 3);
INSERT INTO permutations VALUES(10, 8);
INSERT INTO permutations VALUES(11, 2);
INSERT INTO permutations VALUES(11, 4);
INSERT INTO permutations VALUES(11, 7);
INSERT INTO permutations VALUES(12, 2);
INSERT INTO permutations VALUES(12, 4);
INSERT INTO permutations VALUES(12, 8);
INSERT INTO permutations VALUES(13, 2);
INSERT INTO permutations VALUES(13, 5);
INSERT INTO permutations VALUES(13, 7);
INSERT INTO permutations VALUES(14, 2);
INSERT INTO permutations VALUES(14, 5);
INSERT INTO permutations VALUES(14, 8);
INSERT INTO permutations VALUES(15, 2);
INSERT INTO permutations VALUES(15, 6);
INSERT INTO permutations VALUES(15, 7);
INSERT INTO permutations VALUES(16, 2);
INSERT INTO permutations VALUES(16, 6);
INSERT INTO permutations VALUES(16, 8);

但是,请你帮忙做一下吗?在mysql中我们可以创建一个游标(CREATE临时表)并插入其中。

我怎么能在posgresql中做到这一点?我将以

运行它
SELECT * FROM get_things_by_id(ARRAY[1,3]);

DROP function get_things_by_id(ids integer[]);

CREATE OR REPLACE FUNCTION get_things_by_id(ids integer[])
  RETURNS table(pr INTEGER, cons INTEGER) AS $$
BEGIN

CREATE TEMP TABLE found_items ON COMMIT DROP AS(
  id INTEGER
);

CREATE TEMP TABLE found_unique_things ON COMMIT DROP AS(
  id INTEGER
);

DECLARE
  temp_result found_items;

DECLARE
  temp_result_found_unique_things found_unique_things;

SELECT id INTO temp_result FROM ids;

for i in temp_result:
    SELECT id INTO temp_result_found_unique_things FROM permutations where  
    WHERE unique_thing_id = $0') using i;

for g in temp_result_found_unique_things:
    make another selections


END
$$ LANGUAGE plpgsql;

我已创建此功能,但它显示错误:

ERROR:  syntax error at or near "problem_id"
LINE 6:   problem_id INTEGER,

我不明白为什么会发生这种情况,因为没有关于错误的详细信息。

1 个答案:

答案 0 :(得分:1)

代码非常混乱。

  • PostgreSQL plpgsql函数必须具有documentation中描述的语法。

  • 循环FOR i IN temp_result:看起来像Python循环,而不像PLpgSQL cycle

  • 创建临时表不能位于函数体的DECLARE部分。这些语句应该在函数体中(在BEGIN关键字之后)。

  • Postgres中的数组和表是不同的对象,你不能混用它们。

  • 不要将compose变量用于单个字段值 - 它的速度较慢。

  • 条款USING应在EXECUTE声明中使用。

它正在寻找,所以你想迭代数组

CREATE OR REPLACE FUNCTION foo(ids int[])
RETURNS TABLE(...) AS $$
DECLARE
   i int;
   temp_result_id int;
BEGIN
  FOREACH i IN ARRAY ids
  LOOP
    SELECT id INTO temp_result_id FROM permutations
      WHERE unique_thing_id = i;
    ...
  END LOOP;
END;
$$ LANGUAGE plpgsql;

有一个很大的规则 - SQL可以做什么,应该用SQL来完成。。对于这种情况,您不需要对数组进行迭代 - 只需使用=ANY运算符并减少嵌套循环的数量:

SELECT .. FROM tab WHERE unique_thing_id = ANY(ids)