English 中文(简体)
插入从触发器中插入的多个记录( O)
原标题:Inserting multiple records from a trigger in oracle

i created a trigger to insert the employeeIDs whenever a position is updated. In some cases, there are many employees attached to one position so the trigger isnt able to insert all the employees (only 1). i need all the employee Ids

有谁能帮我解码吗?

观 观 观

create or replace trigger postn_updt
after update on postn
for each row

declare 

cursor m is 
select u.login
from user u,
party_per s
where u.row_id=s.person_id 
and s.party_id=:new.row_id;

rownum varchar2(10);

begin
if updating ( postn_type_cd ) then
open mult;
fetch mult into rownum;

insert into test123
(employee_number,type,request_date,remarks)
values
(( rownum,
 Updated ,sysdate,  
);
close m;
end if;
end;
问题回答

触发器! @ label

如果你将应用程序代码嵌入像这样的触发器,那么维持和调试将是可怕的,而且你总是会遇到这样的情况:基于触发的方法由于变异表格错误而不能奏效。

您最好只为审计和其他不应用活动保留触发器,并将这种逻辑纳入应用本身。

,插入多个行,您需要 LOOP 或某种形式的“INSTERT... sELECT” 语句。

eg. 缩略语

create or replace trigger postn_updt
after update on postn
for each row

declare 

cursor m is 
select u.login
from user u,
party_per s
where u.row_id=s.person_id 
and s.party_id=:new.row_id;

begin
if updating ( postn_type_cd ) then

  for mult_rec in  m LOOP

    insert into test123
    (employee_number,type,request_date,remarks)
    values
    (( mult_rec.login,
     Updated ,sysdate,  
    );
  END LOOP;

end if;
end;

create or replace trigger postn_updt
after update on postn
for each row

declare 
begin
if updating ( postn_type_cd ) then

    insert into test123
    (employee_number,type,request_date,remarks)
    select u.login , Updated ,sysdate,  
    from user u,
        party_per s
    where u.row_id=s.person_id 
    and s.party_id=:new.row_id;

end if;
end;




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签