SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Plpgsql Simple Question

    How can I call one function inside another on a Plpgsql function from PostgreSQL and put the return result inside a variable?

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Los Angeles
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's pretty easy. Declare a variable as the same type that is returned from the function and just assign it the result of the function. A quick example:
    Code:
    DECLARE
        my_result TEXT;
    
    BEGIN
        my_result := my_other_func();
        -- do something with my_result;
    END;
    Last edited by kuato; Dec 17, 2004 at 00:57.

  3. #3
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kuato
    It's pretty easy. Declare a variable as the same type that is returned from the function and just assign it the result of the function. A quick example:
    Code:
     DECLARE
         my_result TEXT;
     
     BEGIN
         my_result := my_other_func();
         -- do something with my_result;
     END;
    Oh, thanks. I'll try that!

  4. #4
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I need to pass a parameter to the function, then it doesn't work:
    Code:
      CREATE OR REPLACE FUNCTION createProjectInfo( integer ) RETURNS void AS '
      DECLARE
      	insert_project ALIAS FOR $1;
      	insert_section integer;
      BEGIN
      	insert_section := getSectionId(\'ProjectInfo\');
      	INSERT INTO 
      		project_info(project_id, section_id) 
      	VALUES 
      		(insert_project, insert_section);
      	RETURN;
      END'
      LANGUAGE 'plpgsql';
    This code returns: "null value in column "section_id" violates not-null constraint". Which means that the insert_section := getSectionId(\'ProjectInfo\') isn't working. I've tried with double mark quotes, but didn't work neighter.

    ps: getSectionId() is a function and it returns an integer.

  5. #5
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, it's working now.

    getSectionId was wrong


Bookmarks

Posting Permissions

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