0

I am typing a function and i'm having an error here, I dont know what it is. Could you give me a Hand ?

CREATE or replace FUNCTION function1(pIdReg in number,pIdPeriod in number) RETURN 

number
IS
  ncv number DEFAULT 0;
BEGIN
     SELECT COUNT(IdPeriod)
         INTO ncv
         FROM(
             SELECT a.IdPeriod, SUM(case when a.nt=0 then -a.valor else a.valor end) AS total --IF(a.nt=0,-a.valor,a.valor))
             FROM dc a
             JOIN emp b ON a.idDoc = b.idDoc
             WHERE a.idReg = pIdReg AND a.IdPeriod < pIdPeriod AND
             b.cc != 305 AND
                  (
                    b.cc = 302 AND(b.tipomov != 4)
                    OR
                    b.cc != 302 AND(1=1)-- emular el TRUE
                  )
             AND a.type != 7
             GROUP BY 1 HAVING total != 0
             ) AS ncv;
      RETURN ncv;
END;
/

The error is SQL command not properly ended. Sqldeveloper shows "AS ncv" underlined. Is there any problem with group by or having clause ?

1
  • Simply remove the AS keyword. Oracle does not allow this for a table alias. Commented Feb 1, 2013 at 14:24

1 Answer 1

4

I see three errors (though there may be more)

  • Oracle does not use the AS keyword for assigning table aliases. So AS ncv is invalid. If you want to use ncv as the alias for your subquery, you'd need to remove the AS (though it seems odd to choose an alias that happens to collide with the name of a local variable).
  • You cannot use positional notation in a GROUP BY clause. You would need to specify the name of the column(s) you want to group by not their position.
  • You cannot use aliases defined in your SELECT list in your HAVING clause. You would have to specify the aggregate function in the HAVING clause

Putting those three things together, I suspect you want something like this

CREATE or replace FUNCTION function1(pIdReg in number,pIdPeriod in number) 
  RETURN number
IS
  ncv number DEFAULT 0;
BEGIN
  SELECT COUNT(IdPeriod)
    INTO ncv
    FROM(
         SELECT a.IdPeriod, 
                SUM(case when a.nt=0 
                         then -a.valor 
                         else a.valor 
                     end) AS total --IF(a.nt=0,-a.valor,a.valor))
           FROM dc a
           JOIN emp b ON a.idDoc = b.idDoc
          WHERE a.idReg = pIdReg AND a.IdPeriod < pIdPeriod 
            AND b.cc != 305 
            AND (
                  b.cc = 302 AND(b.tipomov != 4)
                  OR
                  b.cc != 302 AND(1=1)-- emular el TRUE
                )
           AND a.type != 7
         GROUP BY a.IdPeriod
        HAVING SUM(case when a.nt=0 
                        then -a.valor 
                        else a.valor 
                    end) != 0
       ) ncv;
  RETURN ncv;
END;
/

If you are still getting errors, it would be extremely helpful if you could edit your question and provide the DDL to create the tables that are referenced in this code. That would allow us to test on our systems whether the function compiles or not rather than trying to guess at the syntax errors

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.