Can someone help me to fix an error pls-00049? - oracle

i have created a trigger to my Oracle task and got some errors and i'm trying to solve but don't know how. I was searching fix to my error but i didn't find helpful solution
Is someone can help me to do it, please?
So here's my code:
create table sourcetablename_log (
id number,
operation_date date,
old_username varchar2(20),
new_username varchar2(20),
old_first_name varchar2(20),
new_first_name varchar2(20),
old_last_name varchar2(20),
new_last_name varchar2(20),
action varchar2(20),
author varchar2(20)
);
create or replace trigger src_log
before insert or delete or update on sourcetablename_log
for each row
begin
if inserting then
insert into sourcetablename_log(id, operation_date, new_username, new_firstname, new_lastname)
values (:new.id, sysdate, :new.username, :new.firstname, :new.lastname);
elsif updating then
insert into sourcetablename_log(id, operation_date, old_username, new_username, old_firstname, new_firstname, old_lastname, new_lastname)
values (:new.id, sysdate, :old.username, :new.username, :old.firstname, :new.firstname, :old.lastname, :new.lastname);
elsif deleting then
insert into sourcetablename_log(id, operation_date, old_username, new_username, old_firstname, new_firstname, old_lastname, new_lastname)
values (:new.id, sysdate, :old.username, :new.username, :old.firstname, :new.firstname, :old.lastname, :new.lastname);
end if;
end src_log;
And here's an error:
LINE/COL ERROR
--------- -------------------------------------------------------------
4/27 PLS-00049: bad bind variable 'NEW.USERNAME'
4/42 PLS-00049: bad bind variable 'NEW.FIRSTNAME'
4/58 PLS-00049: bad bind variable 'NEW.LASTNAME'
7/27 PLS-00049: bad bind variable 'OLD.USERNAME'
7/42 PLS-00049: bad bind variable 'NEW.USERNAME'
7/57 PLS-00049: bad bind variable 'OLD.FIRSTNAME'
7/73 PLS-00049: bad bind variable 'NEW.FIRSTNAME'
7/89 PLS-00049: bad bind variable 'OLD.LASTNAME'
7/104 PLS-00049: bad bind variable 'NEW.LASTNAME'
10/27 PLS-00049: bad bind variable 'OLD.USERNAME'
10/42 PLS-00049: bad bind variable 'NEW.USERNAME'
10/57 PLS-00049: bad bind variable 'OLD.FIRSTNAME'
10/73 PLS-00049: bad bind variable 'NEW.FIRSTNAME'
10/89 PLS-00049: bad bind variable 'OLD.LASTNAME'
10/104 PLS-00049: bad bind variable 'NEW.LASTNAME'
Errors: check compiler log
My problem is:
Automate creation of logging table and associated with it triggers A Create a stored procedure that creates a LOGGING table with corresponding columns of OLD values and NEW values, ACTION column, that should be filled in by type of an ACTION and user name (AUTHOR)
Example: You have a table USER of users with next columns:
USERNAME VARCHAR
FIRST_NAME VARCHAR
LAST_NAME VARCHAR
Your procedure should produce a new table with name of “SOURCETABLENAME”_LOG - in this case - USER_LOG with next columns:
ID NUMBER [or OPERATION_ID NUMBER]
OPERATION_DATE DATE
OLD_USERNAME VARCHAR
NEW_USERNAME VARCHAR
OLD_FIRST_NAME VARCHAR
NEW_FIRST_NAME VARCHAR
OLD_LAST_NAME VARCHAR
NEW_LAST_NAME VARCHAR
ACTION VARCHAR
AUTHOR VARCHAR**

If column name is e.g. new_username, then you'd use :new.new_username, not just :new.username.
The same goes for all other columns.
For example:
insert into sourcetablename_log(id, operation_date, new_username, new_firstname, new_lastname)
values (:new.id, sysdate, :new.new_username, :new.new_firstname, :new.new_lastname);

Related

inserting into relational table from entity table

Hi I want to create a trigger which will insert values on relational table A_EQ, A_U and A_P after inserting the values to entity table ACCOUNTS. The values will the inserted into A_EQ if only the ACCOUNTS.TYPE='Equipments', into A_U if only ACCOUNTS.TYPE='Utility' and into A_P for the rest.
Whenever I'm trying to compile the trigger certain errors are showing:
Error(40,13): PLS-00049: bad bind variable 'NEW.A_U'
Error(41,13): PLS-00049: bad bind variable 'NEW.A_U'
Error(42,13): PLS-00049: bad bind variable 'NEW.A_U'
Error(45,9): PLS-00049: bad bind variable 'NEW.A_EQ'
Error(46,13): PLS-00049: bad bind variable 'NEW.A_EQ'
Error(47,13): PLS-00049: bad bind variable 'NEW.A_EQ'
Error(50,9): PLS-00049: bad bind variable 'NEW.A_P'
Error(51,13): PLS-00049: bad bind variable 'NEW.A_P'
Error(52,13): PLS-00049: bad bind variable 'NEW.A_P'
Error(57,4): PLS-00103: Encountered the symbol ";" when expecting one of the following: if
The Code for trigger is given below:
create or replace trigger accounts_relational_insert
after insert on accounts
REFERENCING NEW AS new OLD AS Old
for each row
declare
sys_month VARCHAR2 (10) ;
i_month VARCHAR2 (10);
old1 int;
curr_amount number (20,2);
curr_id varchar2 (20);
curr_txn varchar2 (20);
curr_type varchar2 (20);
begin
select to_char(SYSDATE,'Month') into sys_month from dual;
select ACCOUNTS_ID_SEQ.currval into old1 from dual;
select to_char(paid_on,'Month') into i_month from accounts where transaction_id ='Txn' || lpad(old1,9,'0');
select amount into curr_amount from accounts where transaction_id='Txn' || lpad(old1,9,'0');
select id into curr_id from accounts where transaction_id='Txn' || lpad(old1,9,'0');
select transaction_id into curr_txn from accounts where transaction_id='Txn' || lpad(old1,9,'0');
select type into curr_type from accounts where transaction_id='Txn' || lpad(old1,9,'0');
if i_month = sys_month then
if curr_type ='Utility' then
:new.a_u.transaction_id := curr_txn;
:new.a_u.u_id :=curr_id;
:new.a_u.amount := curr_amount;
else if curr_type='Equipments' then
:new.a_eq.transaction_id := curr_txn;
:new.a_eq.eq_id :=curr_id;
:new.a_eq.amount := curr_amount;
else
:new.a_p.transaction_id := curr_txn;
:new.a_p.barcode:=curr_id;
:new.a_p.amount := curr_amount;
end if;
end if;
end;
The Tables are given below:
It will be very helpful if someone tells me how to resolve this error and also if there is any other way to insert into relational tables after inserting on accounts table.
You can rearrange the trigger simply as simple as this one
CREATE OR REPLACE TRIGGER accounts_relational_insert
AFTER INSERT ON accounts
FOR EACH ROW
BEGIN
IF TO_CHAR(:new.paid_on, 'YYYYMM') = TO_CHAR(sysdate, 'YYYYMM') THEN
IF :new.type = 'Utility' THEN
INSERT INTO a_u VALUES(:new.id, :new.transaction_id, :new.amount);
ELSIF :new.type = 'Equipments' THEN
INSERT INTO a_eq VALUES(:new.id, :new.transaction_id, :new.amount);
ELSE
INSERT INTO a_p VALUES(:new.id, :new.transaction_id, :new.amount);
END IF;
END IF;
END;
/
where
the values, which are already been able to get from identifiers of
columns of the table accounts qualified with :new , to be
inserted shouldn't return from SELECT statements. Moreover, you would get mutating
trigger error for this case.
indeed, defining the local variables are not needed due to the fact
that expressed above, and also values for i_month and sys_month
will directly be derived from TO_CHAR(:new.paid_on, 'YYYYMM') and
TO_CHAR(sysdate, 'YYYYMM') respectively. Btw, do not prefer using string
expression within character conversion such as Month, since you might not
only face case sensitivity issues, but also might fail for international
studies related to language differences.
the trigger is created on accounts table, for the other tables you
should use INSERT statements, cannot use such variable assignments.
The expression REFERENCING NEW AS new OLD AS old is redundant, as
being default.

Bad bind variable in trigger(in one env working in another it is not)

I have below item table in which I have applied trigger.
Name Null? Type
---------- ----- -------------
ID NUMBER(10)
ENTITYNAME VARCHAR2(100)
SYNONYMS VARCHAR2(100)
I am inserting data into dynamicentity table. structure is below.
Name Null? Type
---------- ----- ------------
ID NUMBER(38)
ENTITYNAME VARCHAR2(45)
SYNONYMS VARCHAR2(45)
OPERATION VARCHAR2(10)
TABLENAME VARCHAR2(45)
Whenever I am doing dml in item table value is getting inserted into dynamicentity table.
Below is the trigger. It is compiling in one env but not in another env.
set define off;
Create or replace trigger item_trigger
before insert or update or delete on item
for each row
begin
If inserting then
Insert into dynamicentity values(:new.id,:new.entityname,:new.synonyms,'add','ItemEntity');
end if;
if deleting then
Insert into dynamicentity values(:old.id,:old.entityname,:old.synonyms,'delete','ItemEntity');
end if;
if updating then
Insert into dynamicentity values(:new.id,:new.entityname,:new.synonyms,'modify','ItemEntity');
end if;
End item_trigger;
I am getting below error while compiling trigger
Trigger ITEM_TRIGGER compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
3/34 PLS-00049: bad bind variable 'NEW.ID'
3/42 PLS-00049: bad bind variable 'NEW.ENTITYNAME'
6/34 PLS-00049: bad bind variable 'OLD.ID'
6/42 PLS-00049: bad bind variable 'OLD.ENTITYNAME'
9/34 PLS-00049: bad bind variable 'NEW.ID'
9/42 PLS-00049: bad bind variable 'NEW.ENTITYNAME'
Errors: check compiler log

Oracle SQL PLS-00049: bad bind variable on Trigger

I'm getting this error i try to fix it without success, i have controlled the table that i use to to this trigger but it doesn't work.
this is the code,
create or replace TRIGGER INSERISCITURNO
instead of insert on VistaTurno for each row
declare
ok boolean;
begin
ok := false;
for i in (select a.Giocatore from AVATAR a where a.Partita = :new.Partita ) loop
if (:new.Giocatore = i.Giocatore) then
insert into TURNO(CodTurno, NumTurno, Partita, Giocatore) values (:new.CodTurno, :new.NumTurno, :new.Partita, :new.Giocatore);
AggiornaSituazioneGiocatore(:new.Giocatore, :new.CodTurno, :new.Partita);
ok := true;
end if;
end loop;
if(ok = false) then
dbms_output.put_line('Il giocatore è in partita, inserimento fallito!');
end if;
end;
they give me this error on compilation,
Errore(8,68): PLS-00049: bad bind variable 'NEW.PARTITA'
Errore(9,11): PLS-00049: bad bind variable 'NEW.GIOCATORE'
Errore(10,75): PLS-00049: bad bind variable 'NEW.CODTURNO'
Errore(10,90): PLS-00049: bad bind variable 'NEW.NUMTURNO'
Errore(10,105): PLS-00049: bad bind variable 'NEW.PARTITA'
Errore(10,119): PLS-00049: bad bind variable 'NEW.GIOCATORE'
Errore(11,39): PLS-00049: bad bind variable 'NEW.GIOCATORE'
Errore(11,55): PLS-00049: bad bind variable 'NEW.CODTURNO'
Errore(11,70): PLS-00049: bad bind variable 'NEW.PARTITA'
pls, can someone help me? i think the syntax is good.
add VistaTurno
CREATE OR REPLACE FORCE VIEW VISTATURNO (
"CODTURNO", "NUMTURNO", "PARTITA", "GIOCATORE"
) AS
select * from TURNO;
and this is the Turno
create table TURNO (
CodTurno integer primary key ,
NumTurno integer not null ,
Partita integer,
Giocatore integer
);
alter table TURNO
add constraint v4 check (NumTurno > 0);
alter table TURNO
add constraint fk1 FOREIGN KEY (Partita) REFERENCES PARTITA(CodPartita);
alter table TURNO
add constraint fk2 FOREIGN KEY (Giocatore) REFERENCES
GIOCATORE(CodGiocatore);
enter image description here

I do not know how to create log trigger

I need to create log trigger for after update, after insert and before delete.
In the accounts_history table I have more rows than in the table of accounts and it confuses me
How to write that trigger?
I tried to do it but I did not succeed, This is how I created tables and sequences.
I'm a beginner in Oracle and plsql
I'm sorry if I did not explain my problem well.
create table accounts (
id number,
name varchar2(32),
amount number,
date date
);
create sequnce acc_seq_id
start wtih 1
increment by 1
nocache
nocycle;
create table accounts_history (
id number
, old_name varchar2(32)
, new_name varchar2(32)
, old_amount number
, new_amount number
, change_date date
);
My trigger for only after update
create or replace trigger after_update
after update
on accounts referencing new as new old as old
for each row
begin
iNSERT INTO account_history
(
id,
name,
old_name,
amount,
old_amount,
date
)
values
(
:old.id,
:new.name,
:old.old_name,
:new.amount,
:old.old_amount,
sysdate
);
end;
/
The error:
SQL> show error
Errors for TRIGGER AFTER_UPDATE:
LINE/COL ERROR
-------- --------------------------------------------------------------
2/1 PL/SQL: SQL Statement ignored
9/2 PL/SQL: ORA-01747: invalid user.table.column, table.column, or
column specification
13/2 PLS-00049: bad bind variable 'OLD.ID'
14/2 PLS-00049: bad bind variable 'NEW.NAME'
15/2 PLS-00049: bad bind variable 'OLD.OLD_NAME'
16/2 PLS-00049: bad bind variable 'NEW.AMOUNT'
17/2 PLS-00049: bad bind variable 'OLD.OLD_AMOUNT'
SQL>
The values you insert come from the ACCOUNTS table, the columns you target come ACCOUNT_HISTORY table. You have mixed them up, which is why you get the ORA-01747 error. Try this:
create or replace trigger after_update
after update on accounts
referencing new as new old as old
for each row
begin
INSERT INTO account_history
(
id,
new_name,
old_name,
new_amount,
old_amount,
change_date
)
values
(
:old.id,
:new.name,
:old.name,
:new.amount,
:old.amount,
sysdate
);
End;
/

Error(14,7): PLS-00049: bad bind variable

I have tried everything but Oracle 11g Express Edition gives me this error for the procedure.
Error(14,7): PLS-00049: bad bind variable
Here is my procedure
create or replace procedure select_member_data
AS
memberdata MEMBER%rowtype;
member_ID MEMBER.MEMBER_ID%TYPE;
first_name MEMBER.FIRST_NAME%TYPE;
last_name MEMBER.LAST_NAME%TYPE;
address MEMBER.ADDRESS%TYPE;
total_rows number(2);
BEGIN
SELECT MEMBER_ID,first_name,last_name,address
INTO :memberdata
FROM MEMBER where member_id= 1 ;
IF sql%notfound THEN
dbms_output.put_line('no records fetched');
ELSIF sql%found THEN
dbms_output.put_line( total_rows ||' fetched');
END IF;
END;
here is my table
CREATE TABLE MEMBER (
MEMBER_ID INT PRIMARY KEY,
LAST_NAME VARCHAR2(25) NOT NULL,
FIRST_NAME varchar2(25),
ADDRESS varchar2(100),
CITY varchar2(30),
PHONE varchar2(15),
JOIN_DATE date DEFAULT SYSDATE NOT NULL
);
You are doing:
SELECT MEMBER_ID,first_name,last_name,address
INTO :memberdata
...
but memberdata is a local PL/SQL variable, not a bind variable, so it should just be:
SELECT MEMBER_ID,first_name,last_name,address
INTO memberdata
...
You also aren't fetching enough columns into your %rowtype variable. You table has seven columns, so your memberdata record variable has seven fields as well; but your query is only selecting four columns - which isn't enough. (Slightly confusingly, this is reported as too many values, rather than not enough values).
You can list all of the column names, but this is a rare occasion where it may be better (or at least justifiable) to use *:
SELECT *
INTO memberdata
...
Or as you have the other variables defined already you could do:
SELECT MEMBER_ID,first_name,last_name,address
INTO member_ID, first_name, last_name, address
...
although I'd stongly recommend you don't have variable names that are the same as column names; it's common to prefix local variable with something to identify them, e.g. l_member_id.
Your logic is flawed though. If your query doesn't get exactly one row then a no-data-found or too-many-rows exception will be thrown. (You can't have more than one because of the primary key here, but you could find none). The sql%notfound check won't be reached.

Resources