I'm trying the update a table using the function below. The table name is a function argument. Running this function results in the error:
ERROR: syntax error at or near "$1"
LINE 1: (SELECT * FROM $1 ORDER BY $2 )
I tried using the EXECUTE statement and the quote_ident function, but without success sofar. I'm sure I'm overlooking something simple...
CREATE OR REPLACE FUNCTION createdefaultorder(table_name varchar, sort_column varchar)
RETURNS integer AS
$BODY$
DECLARE
rRec RECORD;
counter integer := 0;
BEGIN
FOR rRec IN (SELECT * FROM table_name ORDER BY sort_column) LOOP
UPDATE table_name SET row_number = counter WHERE id = rRec.id;
counter := counter + 1;
END LOOP;
RETURN 0;
END;
$BODY$
LANGUAGE plpgsql;