开发者

Save SQL query to variable

I have written a PL/PgSQL trigger and i need to save the query (in fact the result set) to variable. See below:

DECLARE
    __query record;
    r record;
BEGIN
    __query := (SELECT * FROM posts);
    FOR r IN __query LOOP
        -- do something with the row data
    END LOOP;

    RETURN NEW;
END;

Which data type for query itself should i use?

I guess record is not appropriate data type and should be us开发者_运维技巧ed in the loop cycle itself (for r var).


If you mean you want to pass the query for the loop as a character variable, then you can do it like this:

DECLARE 
    _query : text;
    r : record;
BEGIN
    _query := 'SELECT * FROM posts';

    FOR r IN EXECUTE _query LOOP
       -- do stuff
    END LOOP;

    RETURN new;
END;


DECLARE

  CURSOR cursor is (select * from posts);
  r      record;
BEGIN

FOR cursor_rec in cursor LOOP
  ... 
END LOOP;


DECLARE

  cursor c is select * from posts;
  -- r  record; you don't need to declare this variable
BEGIN
   FOR c_rec in c LOOP
       ... 
       ... 
       ... 
   END LOOP;
END;

this code for oracle pl-sql

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜