Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2005
    Location
    Barcelona
    Posts
    42

    Unanswered: Recursive procedures / functions



    Counting from 1 to 5 recursively...


    * Recursive procedure (OK):


    CREATE PROCEDURE suma (IN number INTEGER)
    LANGUAGE SQL
    BEGIN

    DECLARE error_message CHAR(30);
    DECLARE procedure_call CHAR(20);

    SET number = number + 1;
    IF number = 5 THEN
    SET error_message = 'OK 5 recusive calls!';
    SIGNAL SQLSTATE '75002'
    SET MESSAGE_TEXT = error_message;
    END IF;

    -- DB2 recursive procedure call
    SET procedure_call = 'CALL suma(?)';
    PREPARE proc_exec FROM procedure_call;
    EXECUTE proc_exec USING number;
    END;


    CALL suma(0);


    * Recursive function (ERROR):


    CREATE FUNCTION suma (number INTEGER)
    RETURNS INTEGER
    NO EXTERNAL ACTION NOT DETERMINISTIC READS SQL DATA
    BEGIN ATOMIC

    DECLARE error_message CHAR(30);
    DECLARE function_call CHAR(20);

    SET number = number + 1;
    IF number = 5 THEN
    SET error_message = '5 recusive calls';
    SIGNAL SQLSTATE '75002'
    SET MESSAGE_TEXT = error_message;
    END IF;

    -- DB2 recursive function call
    SET function_call = 'CALL suma(?)';
    PREPARE func_exec FROM function_call;
    EXECUTE func_exec INTO number USING number;
    END;


    CALL suma(0);


    Not recursive functions in DB2?


  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    You can't CALL a function, recursive or not.

    Are you trying to write your own mathlib using DB2 SQL?

Posting Permissions

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