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 ?
I see three errors (though there may be more)
ASkeyword for assigning table aliases. SoAS ncvis invalid. If you want to usencvas the alias for your subquery, you’d need to remove theAS(though it seems odd to choose an alias that happens to collide with the name of a local variable).GROUP BYclause. You would need to specify the name of the column(s) you want to group by not their position.SELECTlist in yourHAVINGclause. You would have to specify the aggregate function in theHAVINGclausePutting those three things together, I suspect you want something like this
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