If statement inside Trigger not working SQL(Oracle) - oracle

My goal is to create a trigger that checks if the number you are trying to enter NR_RECIBO is in the table Doc_cabecalho with the attribute TIPO_DOC = 4
CREATE OR REPLACE TRIGGER ValidaRecibo
BEFORE INSERT ON Recibo
FOR EACH ROW
DECLARE val NUMBER;
BEGIN
SELECT COUNT(*) INTO val
FROM Doc_cabecalho
WHERE (TIPO_DOC = 4 AND NR_DOCUMENTO = :NEW.NR_RECIBO);
IF val = 0
THEN (-20502, ' Only from department 4 ');
END IF;
END ValidaRecibo;
Yet, this raises the following error:
PLS-00103: Encountered the symbol "," when expecting one of the following:
* & = - + < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like like2 like4 likec as between || member submultiset

IF val = 0 THEN
raise_application_error(-20502, ' Only from department 6 ');
And you should decide between DOC_TIPO and TIPO_DOC ;)

Related

What is wrong with declaration variable in function

I am trying to declare function and pass variable fixed_cost = 0 IN INT something like
FUNCTION GET_ALL_ENABLED_ACC(fixed_cost IN INT)
So the point is to declare fixed_cost and initialize value to 0 but I ge error
Error(37,16): PLS-00103: Encountered the symbol " " when expecting one of the following: . ( * # % & = - + ; < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like like2 like4 likec between using || multiset member submultiset The symbol " " was ignored.
I try something like
fixed_cost : = 0 IN INT
AND
fixed_cost =: 0 IN INT
Also I try to change INT to NUMBER and after BEGIN initialize value but doesn't work.
FUNCTION GET_ALL_ENABLED_ACC(fixed_cost NUMBER)
RETURN SYS_REFCURSOR IS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR
fixed_cost = 0;
SELECT Number_ FROM accounting_plan WHERE Enabled = 1 AND FixedCost = fixed_cost;
RETURN rc;
END GET_ALL_ENABLED_ACC;
What should I do in this case ?
It must be FUNCTION GET_ALL_ENABLED_ACC(fixed_cost IN INT:=0) or FUNCTION GET_ALL_ENABLED_ACC(fixed_cost IN INT default 0)

Need solution for ORA-01422: exact fetch returns more than requested number of rows

I am asked to make a stored procedure in Oracle for the below query which would take input parameter as #FID in there where clause to give me the result.
It consists of 4 columns and the result normally is 100+rows for each of the different inputs.
The initial query is below-
SELECT
TJV.FID, TD.F_CLASS_NAME,
CASE
WHEN TJV.job_operation_id = 1 THEN 'INSERT'
WHEN TJV.JOB_OPERATION_ID = 2 THEN 'UPDATE'
WHEN TJV.job_operation_id = 3 THEN 'DELETE'
END AS JOB_OPERATION_TYPE,
TJV.OPERATION_DATE
FROM
TB_JOB_VERSION TJV,
TB_DICTIONARY TD, TB_UFID TU
WHERE
TJV.JOB_ID = #FID
AND TU.FID = TJV.FID
AND TU.F_CLASS_ID = TD.F_CLASS_ID;
I have then made a procedure like this below but it has thrown the below error message -
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at TCGM3D.JOB_EXPLORER, line 8 ORA-06512: at line 6
Code:
CREATE OR REPLACE PROCEDURE job_explorer (
j_fid IN INT
) AS
v_fid NUMBER;
v_f_class_name VARCHAR2(30);
v_job_operation_type VARCHAR(100);
v_operation_date DATE;
BEGIN
SELECT
tjv.fid,
td.f_class_name,
CASE
WHEN tjv.job_operation_id = 1 THEN
'INSERT'
WHEN tjv.job_operation_id = 2 THEN
'UPDATE'
WHEN tjv.job_operation_id = 3 THEN
'DELETE'
END AS job_operation_type,
tjv.operation_date
INTO
v_fid,
v_f_class_name,
v_job_operation_type,
v_operation_date
FROM
tcgm3d.tb_job_version tjv,
tcgm3d.tb_dictionary td,
tcgm3d.tb_ufid tu
WHERE
tjv.job_id = j_fid
AND tu.fid = tjv.fid
AND tu.f_class_id = td.f_class_id;
dbms_output.put_line('FID'
|| v_fid
|| 'F_CLASS_NAME'
|| v_f_class_name
|| 'JOB_OPERATION_TYPE'
|| v_job_operation_type
|| 'OPERATION_DATE'
|| v_operation_date);
END job_explorer;
I have again tried modifying the query using a loop but the results are not tabular and it just comes out like this in the query output window-
"FID85225493
F_CLASS_NAMESCE_EL_TRN_POLE_TBL
JOB_OPERATION_TYPEUPDATE
OPERATION_DATE04-JAN-17
FID251101047
F_CLASS_NAMESCE_EL_SEG_SECTION
JOB_OPERATION_TYPEINSERT
OPERATION_DATE04-JAN-17
FID251101038
F_CLASS_NAMEEL_CONNECTOR
JOB_OPERATION_TYPEINSERT
OPERATION_DATE04-JAN-17
FID251100923
F_CLASS_NAMEEL_PAD
JOB_OPERATION_TYPEINSERT
OPERATION_DATE04-JAN-17"
My final query for this is below-
CREATE OR REPLACE PROCEDURE job_explorer (
j_fid IN INT
) AS
BEGIN
FOR rec IN (
SELECT
tjv.fid,
td.f_class_name,
CASE
WHEN tjv.job_operation_id = 1 THEN
'INSERT'
WHEN tjv.job_operation_id = 2 THEN
'UPDATE'
WHEN tjv.job_operation_id = 3 THEN
'DELETE'
END AS job_operation_type,
tjv.operation_date
FROM
tcgm3d.tb_job_version tjv,
tcgm3d.tb_dictionary td,
tcgm3d.tb_ufid tu
WHERE
tjv.job_id = j_fid
AND tu.fid = tjv.fid
AND tu.f_class_id = td.f_class_id
) LOOP
dbms_output.put_line('FID' || rec.fid);
dbms_output.put_line('F_CLASS_NAME' || rec.f_class_name);
dbms_output.put_line('JOB_OPERATION_TYPE' || rec.job_operation_type);
dbms_output.put_line('OPERATION_DATE' || rec.operation_date);
END LOOP;
END job_explorer;
It would help if anyone could help me out in getting the data in tabular format for each row which i then has to query in a datatable in ado.net.
With this you get answers in tabular form with pipes as delimiter:
CREATE OR REPLACE PROCEDURE job_explorer(j_fid IN INT)
AS
BEGIN
/* header */
dbms_output.put_line( rpad('FID',12)
||'|'||rpad('F_CLASS_NAME',30)
||'|'||rpad('JOB_OPERATION_TYPE',18)
||'|'||rpad('OPERATION_DATE',19)
);
--
/*data*/
FOR rec IN (SELECT tjv.fid,
td.f_class_name,
CASE
WHEN tjv.job_operation_id = 1 THEN
'INSERT'
WHEN tjv.job_operation_id = 2 THEN
'UPDATE'
WHEN tjv.job_operation_id = 3 THEN
'DELETE'
END AS job_operation_type,
tjv.operation_date
FROM tcgm3d.tb_job_version tjv,
tcgm3d.tb_dictionary td,
tcgm3d.tb_ufid tu
WHERE tjv.job_id = j_fid
AND tu.fid = tjv.fid
AND tu.f_class_id = td.f_class_id
) LOOP
--
dbms_output.put_line( lpad(rec.fid,12)
||'|'||rpad(rec.f_class_name,30)
||'|'||rpad(rec.job_operation_type,18)
||'|'||to_char(rec.operation_date,'dd-MON-yy')
);
END LOOP;
END job_explorer;
And output will be:
FID |F_CLASS_NAME |JOB_OPERATION_TYPE|OPERATION_DATE
85225493|SCE_EL_TRN_POLE_TBL |UPDATE |04-JAN-17
251101047|SCE_EL_SEG_SECTION |INSERT |04-JAN-17
251101038|EL_CONNECTOR |INSERT |04-JAN-17
251100923|EL_PAD |INSERT |04-JAN-17

PLS-00103 Encountered symbol ">" error while executing stored prcedure

I am having below stored procedure, when I try to execute it throws error. All the things are proper but don't know why this error it throws. can anybody help me to sort out this.
create or replace PROCEDURE FR_Notes_Mng (
P_STR_ID IN VARCHAR2,
p_Ref_no in VARCHAR2,
P_UserId in VARCHAR2,
P_Note IN VARCHAR2,
P_datestamp IN VARCHAR2,
p_Request_ID in varchar2,
p_WrittenDate IN VARCHAR2
) AS
numSqlCode Number := 0;
RecCounter Number :=0;
strTable varchar2(30) := 'FR_Notes';
strAction varchar2(30) := 'Insert';
vSQLERM VARCHAR2(200) :=SUBSTR(SQLERRM, 1, 85);
BEGIN
Select Count(*) into RecCounter From FR_Notes where str_id=P_str_ID and ref_no=P_Ref_No and user_id=P_UserId and notes=P_note and datestamp=P_Datestamp and to_date(writtendate,'YYYYMMDDHH24MISS')=to_Date(p_WrittenDate,'YYYYMMDDHH24MISS') and request_id=p_request_id;
If RecCounter=0 then
insert into Fr_Notes Values
(p_str_ID,p_ref_no,p_UserId,P_note,p_Datestamp,to_date(p_WrittenDate,'YYYYMMDDHH24MISS'),p_Request_ID,'FR',sysdate);
commit;
end if;
EXCEPTION
WHEN OTHERS THEN
numSqlCode := SQLCODE;
INSERT INTO FR_UNEXPECTED_ERRORS (TABLE_NAME, KEY, ACTION, ERR_CODE)
VALUES (strTable, vSQLERM, strAction, numSqlCode);
END;
Error Thrown:
ORA-06550: line 1, column 47: PLS-00103: Encountered the symbol ">" when expecting one of the following: . ( ) , * # % & = - + < / > at in is
mod remainder not rem <> or != or ~= >= <= <>
and or like like2 like4 likec between || multiset member submultiset
When I execute it at database level its executed properly but failed in vbscript code. Below is the vb-script code I used to execute and which is throwing this error
function InsertNotes(str_id,ref_no,userId,Note,strdatestamp,writtenDates)
Dim strcon2: set strcon2=server.CreateObject("ADODB.Connection")
Dim sql2
Dim strcmd2
strcon2.open "Provider=MSDAORA;Data Source="&Application("DBDsn")&";User Id="&Application("DBUserName")&"; Password="&Application("DBPassword")&";"
sql2 = "rep2.FR_Notes_Mng"
Set strcmd2 = Server.CreateObject("ADODB.Command")
Set strcmd2.ActiveConnection = strCOn2
strcmd2.CommandText = sql2
strcmd2.CommandType = 4
strcmd2.Parameters.Append strcmd2.CreateParameter("p_str_id", 200,1,50,str_id)
strcmd2.Parameters.Append strcmd2.CreateParameter("p_ref_no", 200,1,50,ref_no)
strcmd2.Parameters.Append strcmd2.CreateParameter("p_UserId", 200,1,50,userId)
strcmd2.Parameters.Append strcmd2.CreateParameter("p_note", 200,1,200,Note)
strcmd2.Parameters.Append strcmd2.CreateParameter("p_Datestamp", 200,1,50,strdatestamp)
strcmd2.Parameters.Append strcmd2.CreateParameter("p_Request_id", 200,1,50,"012")
strcmd2.Parameters.Append strcmd2.CreateParameter("p_WrittenDate", 200,1,50,writtenDates)
strcmd2.Execute
end function
Actually I have checked what values are getting passed in VB script call to that stored procedure and found that value for str_id is not getting passed hence the procedure execution was getting failed and throwing above error.
ORA-06550: line 1, column 47: PLS-00103: Encountered the symbol ">" when expecting one of the following: . ( ) , * # % & = - + < / > at in is
mod remainder not rem <> or != or ~= >= <= <>
and or like like2 like4 likec between || multiset member submultiset
I have assigned one value to str_id variable and rechecked by executing the code and it worked properly.
One thing I came to know here by this error which is, when we don't pass required parameter value or we pass the parameter as null even if it is mandatory that time this type of error get generated.
Thanks for all who helped me over this ask.

Error when creating Elsif Trigger Before Update

I am creating trigger when Selisih column is equal to 0, then Status column become 'Partial'. If QtyInternalUse column is equal to QtyRequested, then Status column is Completed, else, it is Not Issued
CREATE OR REPLACE TRIGGER TG_STATUSINREQ
BEFORE UPDATE OR INSERT ON M_INTERNALREQUESTERLINE
FOR EACH ROW
BEGIN
IF (:new.Selisih <> 0 ) THEN
:new.Status := 'Partial';
ELSIF :new.QtyInternalUse := :new.QtyRequested THEN
:new.Status := 'Completed';
ELSE
:new.Status := 'Not Issued';
END IF;
END;
I've tried execute it and there's error that says
PLS-00103: Encountered the symbol "=" when expecting one of the
following:
. ( * # % & = - + < / > at in is mod remainder not rem then
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || indicator multiset member submultiset
The symbol "* was inserted before "=" to continue.
I've tried deleting : in = but still it won't work. How can I fix this?
REVISION
I have updated the logic and the := . The trigger was created and it's like this :
BEGIN
IF (:new.QtyInternalUse = 0 ) THEN
:new.Status := 'Not Issued';
ELSIF :new.QtyInternalUse = :new.QtyRequested THEN
:new.Status := 'Completed';
ELSE
:new.Status := 'Partial';
END IF;
END;
This should be pretty easy google-resolve. Still here you go.
ELSIF :new.QtyInternalUse := :new.QtyRequested THEN
You are trying to compare but using assignment operator[:=]. Use equals[=] in stead that will resolve your problem.
ELSIF :new.QtyInternalUse = :new.QtyRequested THEN

Oracle Database PLSQL Error: PLS-00103

I'm writing simple function in Oracle Database 11g that count summary salary for employee. For this we need to count days with specific status. Days is present as fields in table (Day_1, Day_2, ..., Day_30).
But i have got error during compilation:
Error(50,9): PLS-00103: Encountered the symbol ";" when expecting one of the following: :=.(#%;
Code of my package (a place where there is an error marked in code of function f2):
CREATE OR REPLACE PACKAGE pack IS
FUNCTION f1 (id IN NUMBER) return t2 PIPELINED;
FUNCTION f2 (id IN NUMBER) return number;
end pack;
/
CREATE OR REPLACE PACKAGE BODY pack IS
FUNCTION f1 (id IN NUMBER) return t2 PIPELINED IS
name VARCHAR2(50);
num number;
BEGIN
FOR i IN 1 .. id LOOP
SELECT отдел
into name
from отдел
where ид_отдела = i;
select sum(КОЛИЧЕСТВО)
into num
from Таблица_3
join Таблица_2
on Таблица_3.КТО_ПРОДАЛ = Таблица_2.ЧЛВК_ИД
where отдел = i;
PIPE ROW( t1(i, name, num) );
END LOOP;
RETURN;
END f1;
FUNCTION f2 (id IN NUMBER) return NUMBER AS
WorkingDays NUMBER := 0;
CurrentDay VARCHAR2(50);
Salary NUMBER := 120;
Total NUMBER := 0;
BEGIN
FOR i IN 1 .. 30 LOOP
EXECUTE IMMEDIATE 'SELECT День_' || i || ' FROM Таблица_2 WHERE ЧЛВК_ИД = id INTO CurrentDay';
IF WorkingDays IN ('КОМАНДИРОВКА','ВЫХОДНОЙ','ПРАЗДНИК') THEN -- <--- Here
WorkingDays := WorkingDays + 1;
END IF;
END LOOP;
Total : = Salary * WorkingDays;
RETURN Total;
END f2;
end pack;
/
How can i solve this? Maybe the problem is in the logic of the program?
That is an error on parsing, and be aware that it often does not accurately show the error itself, but where the next symbol error occurred. For example:
declare
x number;
y number;
begin
if x = 1 then
y := 1 --I forget to put on the semi-colon to end the line
end if;
end;
You'd expect an error on the missing semi-colon. What you get is:
ORA-06550: line 7, column 2:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
The symbol ";" was substituted for "END" to continue.
Since in your comments you talk about changing things - perhaps to make it more readable for us (and thank you!), it may be obscuring the actual syntax glitch.
Two things I notice:
Total : = Salary * WorkingDays;
That has a space between the : and = that shouldn't be there
and:
FUNCTION f2 (id IN NUMBER) return NUMBER AS
which is normally
FUNCTION f2 (id IN NUMBER) return NUMBER IS

Resources