There is a compilation error in my PL/SQL code [closed] - oracle

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a compilation error in my PL/SQL code, The audit_client table will be updated if any update or delete occurs on client_master
create or replace trigger t1
after update or delete on client_master
for each row
DECLARE
v1 varchar2(10);
v2 varchar2(80);
v3 number(11);
oper varchar2(15);
BEGIN
v1:= :old.CLIENT_NO
v2:= :old.NAME
v3:= :old.BALANCE
if updating then
oper:='update'
insert into audit_client values(v1,v2,v3,oper,v4,v5);
end if;
if deleting then
oper:='delete'
insert into audit_client values(v1,v2,v3,oper,v4,v5);
end if;
end;
/

Why would you declare those v variables at all? Somewhat simplified:
create or replace trigger t1
after update or delete on client_master
for each row
declare
l_oper varchar2(15);
begin
if updating then
l_oper := 'update';
elsif deleting then
l_oper := 'delete';
end if;
insert into audit_client
(client_no, name, balance, oper, col4, col5)
values
(:old.client_no, :old.name, :old.balance, l_oper, null, null);
end;
/
Instead of v4 and v5 I inserted NULL as you never said what should be put in there.
Also, as a good practice, you should always specify column list you're working with. That's why I made up those column names as I don't know their real names, but you do and you should use them.

Related

PLS-00103: Encountered the symbol "USER" when expecting one of the following: [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am trying to make a trigger, it compiles but with 2 errors (same error different location). This is the trigger, I have the table and sequence created
create or replace TRIGGER BIU_APPLICATION
BEFORE INSERT OR UPDATE ON APPLICATION
REFERENCING FOR EACH ROW
BEGIN
IF inserting THEN IF :new.APPLICATION_ID IS NULL THEN
SELECT APPLICATION_ID_SEQ.nextval
INTO :new.APPLICATION_ID
FROM dual;
END IF;
:new.created_by := NVL(apex_application.g_user USER);
:new.created_dt := SYSDATE;
END IF;
IF inserting OR updating THEN
:new.created_by := NVL(apex_application.g_user USER);
:new.created_dt := SYSDATE;
END IF;
END;
The error I get is
PLS-00103: Encountered the symbol "USER" when expecting one of the following: . ( ) , * # % & = - + < / > at in is mod remainder not rem => <an exponent (**)> <> or != or ~= >= <= <> and or default like like2 like4 likec as between from using || multiset member submultiset The symbol "." was substituted for "USER" to continue.
You forgot about "," in NVL:
NVL(apex_application.g_user, USER);
note: you don't need select sequence.nextval by SQL. You can do this like:
:new.APPLICATION_ID := APPLICATION_ID_SEQ.nextval

Function and Triggers in Oracle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
how to do the equivalent code in ORACLE
The code is made for Postgresql.
CREATE FUNCTION emp_stamp() RETURNS trigger AS emp_stamp
BEGIN
-- BODY
END;
$emp_stamp$ LANGUAGE plpgsql;
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
I don't know Postresql. If you could explain what this piece of code does, it might be easier to assist. Meanwhile, see if this example helps.
Function accepts employee's EMPNO and returns its MAX salary from the salaries table (doesn't make much sense, but that replaces your --body comment).
create or replace function emp_stamp (par_empno in emp.empno%type)
return emp.sal%type
is
retval emp.sal%type;
begin
select max(sal)
into retval
from salaries
where empno = par_empno;
return retval;
end;
/
Trigger calls that function and sets employee's emp.sal column to value returned by the function.
create or replace trigger trg_emp_stamp
before insert or update on emp
for each row
begin
:new.sal := emp_stamp(:new.empno);
end;
/

ORA-06550 with alter statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying this code for some test and i got this error ORA-06550
I tried to modify column with set and many things still the same result
CODE :
declare
cursor cur is select comm from emp where comm is not null ;
enreg emp.comm%type;
moyene number;
somme number;
begin
open cur;
loop
fetch cur into enreg;
if emp.comm=enreg.com then
ALTER TABLE emp
set columun
emp.comm := enreg.com*100/emp.comm ;
end if ;
exit when cur%notfound;
end loop;
end;
the expected result is to change every emp.comm with the 10% of it but i got this error
Error :
ORA-06550: line 12, column 1:
PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with
<<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
1. declare
2. cursor cur is select comm from emp where comm is not null ;
3. enreg emp.comm%type;
In SQL, you use an UPDATE statement to change values in a table. ALTER TABLE is used to change the structure of a table, such as by adding columns, etc. You could rewrite your code as follows:
declare
cursor cur is select comm from emp where comm is not null;
enreg emp.comm%type;
moyene number;
somme number;
begin
open cur;
loop
fetch cur into enreg;
exit when cur%notfound;
if emp.comm = enreg then
update emp
set emp.comm = enreg * 100 / emp.comm;
end if ;
end loop;
commit;
end;
Best of luck.

Pass a table name to a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to return one case-insensitive varchar2 from a table given as a parameter.
The database is configured to have as the first column the one I'll look into.
This is what I got to:
create or replace function generic_return(nam varchar2, table_n varchar2)
return varchar2
as
case_insensitive varchar2(300);
case_ins varchar2(300);
colu varchar2(30);
t_nam varchar2(30):=upper(table_n);
cursor point is execute inmediate select colu from t_nam;
cursor col is select column_name from cols where table_name=t_nam;
non_existent_table exception;
begin
case_ins:=upper(rtrim(ltrim(nam)));
open col;
fetch col into colu;
close col;
select column_name from cols where table_name=upper(table_n);
if colu is null then
raise non_existent_table;
end if;
open point;
loop
fetch point into case_insensitive;
exit when point%notfound;
if upper(case_insensitive)=case_ins then
return case_insensitive;
end if;
end loop;
close point;
return null;
end;
/
The function receives what to look for, and the name of the table to look into, then the first variables are to compare them, t_nam is to use the uppercased version of the second parameter... and from that there's the huge mess: I get the column name from the table in col, from cols, and then I try to make a cursor that goes around checking if what the tuple, going by the same modifications, is the first parameter, or not, if it happens to be, then it should return the unmodified version of the one in the table. Otherwise, it should return "null", which I'll treat differently on each procedure that uses it.
By null I mean nothingness, not the string.
Yes, you are right. That's a pretty huge mess. :-)
For starters, you cannot declare an explicit cursor as an execute immediate statement.
Next, if you want the first column in the table, you need to specify
WHERE column_id = 1
and then you can just grab it via a SELECT-INTO, no need for an explicit cursor.
Then you could try something like:
my_cur SYS_REFCURSOR;
BEGIN
...
OPEN my_cur FOR 'SELECT ' || first_col || ' FROM ' || t_name;
LOOP
FETCH my_cur INTO case_insensitive;
EXIT WHEN my_cur%NOTFOUND;
... logic for match ....
END LOOP;
CLOSE my_cur;

How to debug a stored procedure that won't build in Oracle? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Create or replace function get_empsalary_by dept(emp_dept varchar(20))
return number is
Total_salary number(10,2);
emp_dept varchar(20);
emp_salary number(10,2);
begin
select empdept, sum(empsalary) into emp_dept,emp_salary from employe where empdept=emp_dept;
total_salary :=sum(empsalary);
return total_salary;
end;
/
And the error is:
-----------------------------------------------------------------
PLS-00103: Encountered the symbol "DEPT" when expecting one of
the following:
( return compress compiled wrapped
Create or replace function get_empsalary_by_dept(p_emp_dept in employe.empdept%type)
return number
is
Total_salary number(10,2);
emp_dept varchar(20);
emp_salary number(10,2);
begin
select empdept, sum(empsalary)
into emp_dept,emp_salary
from employe
where empdept=p_emp_dept
group by empdept;
-- total_salary := emp_salary;
return emp_salary;
end;
/
Maybe that this can help you any further.
The name of your function cannot contain any spaces. The input parameter (emp_dept) can not be the same as a variable. The variable emp_dept you do not need in this case. The return value can be emp_salary.
Your function is called "get_empsalary_by dept" (there is a space between "by" and "dept") and so the parser complains because it expects after function a valid (i.e. without spaces) function name: instead it finds two strings.

Resources