jl66
asked on
variable issue in trigger oracle 11gr2
Want to write a trigger like below. Is it possible? Definitely it is compiled with errors.
CREATE OR REPLACE TRIGGER SCOTT.T_TRG BEFORE INSERT ON SCOTT.T
FOR EACH ROW
begin
for i in ( select column_name from all_tab_columns where owner = 'SCOTT' and table_name = 'T')
loop
if :new.i is null then
:new.i := 'N';
end if;
end loop;
END;
/
The reason to use the variable i is there are many columns in T in real situation, so if writing the 'if' block, that would be too many. Take an example, we can assume there are 3 columns in T like below
create table scott.T( c1 varchar2(30), c2 varchar2(10), c3 varchar2(20)); How to use the variable in this case?
Can any gurus shed some light on it?
CREATE OR REPLACE TRIGGER SCOTT.T_TRG BEFORE INSERT ON SCOTT.T
FOR EACH ROW
begin
for i in ( select column_name from all_tab_columns where owner = 'SCOTT' and table_name = 'T')
loop
if :new.i is null then
:new.i := 'N';
end if;
end loop;
END;
/
The reason to use the variable i is there are many columns in T in real situation, so if writing the 'if' block, that would be too many. Take an example, we can assume there are 3 columns in T like below
create table scott.T( c1 varchar2(30), c2 varchar2(10), c3 varchar2(20)); How to use the variable in this case?
Can any gurus shed some light on it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot for everyone's tips.
--slightwv: improved and good for none of 'if' block, but still need to list each one of columns in the table.
--sdstuber: already used default,but encountered the situation slightwv pointed out. 12c has improved it, but currently 11gr2.
--johnsone: tried to write the statements as you listed, but got the errors below:
"[Warning] ORA-24344: success with compilation error
28/1 PLS-00428: an INTO clause is expected in this SELECT statement
(1: 0): Warning: compiled but with compilation errors"
Could you please give me a complete example?
--slightwv: improved and good for none of 'if' block, but still need to list each one of columns in the table.
--sdstuber: already used default,but encountered the situation slightwv pointed out. 12c has improved it, but currently 11gr2.
--johnsone: tried to write the statements as you listed, but got the errors below:
"[Warning] ORA-24344: success with compilation error
28/1 PLS-00428: an INTO clause is expected in this SELECT statement
(1: 0): Warning: compiled but with compilation errors"
Could you please give me a complete example?
You cannot dynamically do what you want.
>>Could you please give me a complete example?
That was working code. What it was meant to do was generate the statements necessary for you to copy and paste into your trigger.
It wasn't meant to be placed into your trigger.
>>Could you please give me a complete example?
That was working code. What it was meant to do was generate the statements necessary for you to copy and paste into your trigger.
It wasn't meant to be placed into your trigger.
What I gave you would be the total body of the code. It should be this:
CREATE OR REPLACE TRIGGER SCOTT.T_TRG BEFORE INSERT ON SCOTT.T
FOR EACH ROW
begin
.... paste output from query here ...
END;
/
The error message you posted isn't helpful as there are no select statements in what I gave you.
>>The error message you posted isn't helpful as there are no select statements in what I gave you
Makes perfect sense if the select you provided was pasted into the trigger body as-is.
Makes perfect sense if the select you provided was pasted into the trigger body as-is.
I guess, but I specifically stated:
>> The results of that query is the body of your trigger.
The result is the body, not the query.
>> The results of that query is the body of your trigger.
The result is the body, not the query.
ASKER
Thanks a lot for everyone's help.
insert into t values(null,null);