简体   繁体   English

光标在触发器中

[英]cursor in a trigger

I have an existing TABLE postn_matrix which contains a list of employees and a count of their resp. 我有一个现有的表postn_matrix ,其中包含员工列表及其postn_matrix数。 positions in the organisation. 组织中的职位。

Whenever a position of a user is added or removed, the corresponding count is reflected in the table thro' this trigger (VIA UPDATE) 每当添加或删除用户的职位时,此触发器就会在表格中反映相应的计数(VIA UPDATE)

Now, if there is a new user, he will not have an entry in postn_matrix , so I have to insert a new record for him/her (VIA INSERT). 现在,如果有新用户,那么他在postn_matrix中将没有条目,因此我必须为他/她(VIA INSERT)插入一条新记录。 This needs to be brought in from the BASE TABLE. 这需要从BASE TABLE中引入。

The update seems to be working fine but I am not able to bring in a new user into the table. 更新似乎工作正常,但我无法将新用户加入表格。

I've been trying to handle this case with a cursor. 我一直在尝试使用游标处理这种情况。 But it hasnt been of any help yet. 但这还没有任何帮助。 I'm hoping some expert could show me the light.. :). 我希望一些专家可以向我展示光.. :)。 any other suggestions besides using a cursor will be much appreciated 除了使用光标以外的任何其他建议将不胜感激

CREATE OR REPLACE TRIGGER TRIG1
BEFORE INSERT OR DELETE ON (BASETABLE)
FOR EACH ROW
DECLARE
  cursor c1 is
  select person_id 
  from postn_matrix;

  v_temp varchar2(15);
BEGIN
  IF INSERTING THEN
    open c1;    
    LOOP
      fetch c1 into v_temp;

      if v_temp!=:new.person_id THEN
        insert into POSTN_MATRIX (PERSON_ID)
        VALUES (:new.PERSON_ID);
      else
        UPDATE POSTN_MATRIX
        //this is working fine ;

      END IF;
    end loop;
    close c1;

END
/

Because of the loop (which is missing an exit clause - hopefully you've just lost that translating this into a question) you're going to attempt to insert a record into pstn_matrix for every record the cursor returns, whether there are any matching :new.person_id or not; 由于循环(缺少退出子句-希望您已经失去了将其转换为问题的希望),您将尝试为游标返回的每个记录将一条记录插入pstn_matrix中,无论是否存在匹配项:new.person_id and if there is match you'll also do the update . 如果匹配,您也将update Which probably isn't what you want, and you might get a constraint violation among other things. 这可能不是您想要的,并且您可能会遇到违反约束的情况。 You also aren't setting your counter field - if that is not nullable then that will error. 您也没有设置计数器字段-如果该字段不可为空,则将出错。 But you haven't said what errors, if any, you are getting. 但是您没有说出您正在得到什么错误,如果有的话。

If you must do this through a trigger then you can either check if there is a row for the new person at all: 如果您必须通过触发器来执行此操作,则可以检查是否有新人要排:

DECLARE
  v_temp postn_matrix.person_id%TYPE;
BEGIN
  IF INSERTING THEN
    select max(person_id) into v_temp
    from postn_matrix
    where person_id = :new.person_id;

    if v_temp is null then
      -- no record found, so insert one
      insert into postn_matrix (person_id, position_count)
      values (:new.person_id, 1);
    else
      -- record exists, so update
      update postn_matrix ...
    end if;
  ...

... or use merge . ...或使用merge

But I don't like this model, and you're setting up the potential for data discrepancies with concurrent modifications to the base table. 但是我不喜欢这种模型,并且您正在通过同时修改基表来设置数据差异的可能性。 Trying to maintain a count like this is not necessarily as simple as it seems. 试图保持这种计数不一定像看起来那样简单。

I'd usually prefer to make this a view, which will always be up to date and doesn't need the trigger complicating things: 我通常希望将其设为一个视图,该视图将始终是最新的,并且不需要触发器使事情变得复杂:

create view postn_matrix as
  select person_id, count(*)
  from basetable
  group by person_id;

Of course, I may be misinterpreting or oversimplifying what your base table(s) does and what you need postn_matrix for. 当然,我可能会误解或简化了基表的功能以及postn_matrix需要的postn_matrix It seems a little trivial to have even as a view. 甚至拥有一个视图似乎有点琐碎。 If you have separate person and person_position tables, say, then you can add in an outer join to see people with no positions: 例如,如果您有单独的personperson_position表,则可以添加外部person_position以查看没有职位的人:

create view postn_matrix as
  select p.person_id, count(pp.position_id)
  from person p
  left join person_position pp on pp.person_id = p.person_id
  group by p.person_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM