Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    3

    Unanswered: Creating roles inside stored functions

    When I try to create a role/user inside a stored function passing a function parameter to the role/user name it is interpreted literally, how can I make the create role command recognize the value inside the variable? ex:

    "
    CREATE OR REPLACE FUNCTION work_management.proc_createuser(p_username character varying)
    RETURNS void AS
    $BODY$

    CREATE USER p_username

    $BODY$
    LANGUAGE 'sql' VOLATILE SECURITY DEFINER
    COST 100;
    ALTER FUNCTION work_management.proc_createuser(character varying) OWNER TO root;

    "

    if I use the function as follow :

    select work_management.proc_createuser('testuser');

    an user named 'p_username' is created.

    is there some way to work arround this?

    thanks in advance.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    You will need to build up a string containing the correct statement and then use EXECUTE to run it:

    http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Posting Permissions

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