Postgresql函数如果条件然后运行查询否则停止

时间:2013-10-24 08:14:24

标签: mysql sql arrays postgresql

我需要一些有关以下postgres功能的帮助

我的下表包含以下列:

array, array_length

我最初有一些数组,然后我运行一个查询(实际上是一组3个查询),选择数组,追加它们,然后将附加的数组插入表中。

我需要循环这个插入查询,直到其中一个数组(select max(array_length) from table)达到预定义的大小,比如长度为50。

我基本上需要写一些像

这样的东西
begin 
for (select max(array_length) from table)<50
loop 
(drop table if exists super_item_temp;
ALTER TABLE super_item
    RENAME TO super_item_temp;

create table super_item as 
select distinct * from super_item_temp;

insert into super_item 
select ...calculations... from super_item)
end

但我找不到合适的语法来写这个

我查看了http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html处的手册 但我找不到任何有用的东西。

任何提示或链接指向我正确的方向将不胜感激! 谢谢。

编辑:我试过了

创建或替换FUNCTIOn function_name()RETURNS int4 AS' DECLARE r RECORD;

BEGIN     WHILE(从super_item中选择max(array_length))&lt; 50

loop
drop table if exists super_item_temp;
    ALTER TABLE super_item
        RENAME TO super_item_temp;

    create table super_item as 
    select distinct * from super_item_temp;


    insert into super_item 
    select old_array, 
    array_sort_unique( array_agg(added_item) || a.old_array) as new_array, 
    array_length(array_sort_unique( array_agg(added_item) || a.old_array),1)

    from (
            select 
    a.new_array as old_array,

    case when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
     when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end  as added_item

    from

    super_item a 
    left join pairs b 

    on  string_to_array(b.item1::text, ",")::int[] <@ a.new_array or  string_to_array(b.item2::text, ",")::int[] <@ a.new_array /**any item from pair is in array**/
    where 1=1

    group by a.new_array, 2

    having sum(b.count)>10
    and sum(b.offset)<=0 
    and 
    case 
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end is not null
    /**new item is not null**/
    order by 2 desc
    )a
    group by 1;
END LOOP;

返回1; 结束; 'LANGUAGE plpgsql;

SELECT function_name()作为输出;

现在我收到一个函数参数的错误'unknown column',它应该是一个带引号的分隔符。

  

错误:列“,”existsiert nicht LINE 18:on   string_to_array(b.item1 :: text,“,”):: int []&lt; @ a.new_ar ...

1 个答案:

答案 0 :(得分:1)

试试这个,在我的电脑上它编译得很好(但我不知道它是否按预期工作)。

CREATE OR REPLACE FUNCTION append_super_item ()
RETURNS integer AS $length$
declare
    length integer;
BEGIN
  WHILE (select max(array_length) from super_item)<50
    LOOP
       drop table if exists super_item_temp;
       ALTER TABLE super_item
            RENAME TO super_item_temp;
       Create table super_item as 
         select distinct * from super_item_temp;
       insert into super_item 
    select old_array, 
    array_sort_unique( array_agg(added_item) || a.old_array) as new_array, 
    array_length(array_sort_unique( array_agg(added_item) || a.old_array),1)

    from (
            select 
    a.new_array as old_array,

    case when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
     when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end  as added_item

    from

    super_item a 
    left join pairs b 

    on  string_to_array(b.item1::text, ",")::int[] <@ a.new_array or  string_to_array(b.item2::text, ",")::int[] <@ a.new_array /**any item from pair is in array**/
    where 1=1

    group by a.new_array, 2

    having sum(b.count)>10
    and sum(b.offset)<=0 
    and 
    case 
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end is not null
    /**new item is not null**/
    order by 2 desc
    )a
    group by 1;
    end loop;
end $length$ LANGUAGE plpgsql;