Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Unanswered: Set a variable by select into <variable> max(<expression>)

    I get an ERROR: “query string argument of EXECUTE is null”:
    "loop_count = 20000;" works well.
    "select into loop_count max(index) from players;" returns an error ("select max(index) from players;" returns a number).

    Does anyone know the different ?

    Code:
    CREATE OR REPLACE FUNCTION insert_line(target_index integer)
      RETURNS TABLE(
      index integer
      ) AS
    $BODY$
    	declare	loop_count integer;
    begin
     loop_count  = 20000;
    -- Using the following assignment instead of the above assignment returns an error
    --  select into loop_count max(index) from players;
    	LOOP
    		EXECUTE 'UPDATE players SET index = index + 1	WHERE players.index = ' || loop_count;
    		loop_count = loop_count - 1;
    		IF loop_count < $1 + 1 THEN
    			EXIT;  -- exit loop
    		END IF;
    	END LOOP;
    end;
    $BODY$
      LANGUAGE 'plpgsql' VOLATILE;
    Last edited by Yaira; 08-16-10 at 12:13.

  2. #2
    Join Date
    Aug 2009
    Location
    Olympia, WA
    Posts
    337
    select max(index) into loop_count from players;

  3. #3
    Join Date
    Aug 2010
    Posts
    2
    Thanks .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •