好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

postgresql 循环函数的简单实现操作

我就废话不多说了,大家还是直接看代码吧~

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

create or replace function aa1(a1 integer [],a2 bigint ) returns

void AS $$

declare ii integer ;

declare num integer ;

  begin

  II:=1;

  num = 1;

  FOR ii IN 1..a2 LOOP

  UPDATE student SET

   id=a1[num]

  WHERE cd_id = ii;

  num = num +1;

  if (num>6) then

  num = 1;

  end if;

  end loop;

  end ;

$$ LANGUAGE plpgsql;

 

select aa1(array[1,4,5,6,7,8],6742)

补充:数据库之postgreSql库的存储过程和 循环 总结

postgreSql库中存储过程模板

 

?

1

2

3

4

5

6

7

8

9

10

11

12

CREATE OR REPLACE FUNCTION p_fx_*** ( OUT v_row INTEGER , OUT v_rote varchar (50), OUT v_log varchar (50))

AS $$

DECLARE

BEGIN

 

  select count (*) into v_row from *插入表的名字*;

  v_rote := 'SUCCESS' ;

  v_log := 'SUCCESS' ;

 

END

$$

LANGUAGE plpgsql VOLATILE

postgreSql库中循环书写的模板,以实际开发中的sql为例

 

单层循环

?

1

2

3

4

5

6

do $$

declare ***:=***;

begin

   while *** loop

   end loop;

end $$;

declare --声明变量,如果声明了变量别忘了加分号;

双层循环

?

1

2

3

4

5

6

7

8

9

do $$

declare ***:=***;

begin

   while *循环条件* loop

     for i in 1..12 loop

     raise notice '%' ,*变量名*;

     end loop;

   end loop;

end $$;

raise notice '%',变量名;这是输出语句类似于Java中的print。

将循环放到存储过程中

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

CREATE OR REPLACE FUNCTION p_fx_*** ( OUT v_row INTEGER , OUT v_rote varchar (50), OUT v_log varchar (50))

AS $$

DECLARE

BEGIN

 

while *循环条件* loop

     for i in 1..12 loop

     raise notice '%' ,*变量名*;

     end loop;

   end loop;

 

  select count (*) into v_row from *插入表的名字*;

  v_rote := 'SUCCESS' ;

  v_log := 'SUCCESS' ;

END

$$

LANGUAGE plpgsql VOLATILE

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/u011768325/article/details/50502490

查看更多关于postgresql 循环函数的简单实现操作的详细内容...

  阅读:35次