2

我必须创建一个触发器,该触发器在某个列(小时结束)中不允许多个空值。这是我写的触发器(我使用的是 Sybase):

ALTER TRIGGER "InsertIntoCardDetail" instead of insert on
DBA.CardDetail
referencing new as new_name
for each row
begin
  declare @nullCount integer;
  if(new_name.hourfinish is null) then
    select COUNT(*) into @nullCount
      from CardDetail
      where hourfinish is null;
    if(@nullCount > 0) then
      raiserror 99999 'Cannot have Multiple Nulls'
    else
      insert into CardDetail( card,hourstart,hourfinish,"work",project,N) 
        values( new_name.card,new_name.hourstart,new_name.hourfinish,new_name."work",new_name.project,new_name.N) 
    end if
  else
    insert into CardDetail( card,hourstart,hourfinish,"work",project,N) 
      values( new_name.card,new_name.hourstart,new_name.hourfinish,new_name."work",new_name.project,new_name.N) 
  end if
end

触发器工作正常。我要问的是是否有执行插入的命令 - 可以替换那些长插入语句的命令。

4

2 回答 2

1

尝试将触发器更改为:

ALTER TRIGGER "InsertIntoCardDetail" instead of insert on
DBA.CardDetail
referencing new as new_name
for each row
begin
   if(new_name.hourfinish is null) then    
    if exists 
    (select 1
      from CardDetail
      where hourfinish is null) then
      raiserror 99999 'Cannot have Multiple Nulls'
      return 1
    end if --exists 
   end if--(new_name.hourfinish is null) 

    insert into CardDetail( card,hourstart,hourfinish,"work",project,N) 
    values(new_name.card,new_name.hourstart,new_name.hourfinish,new_name."work",new_name.project,new_name.N) 

end
于 2012-10-01T09:48:29.240 回答
0

我使用插入前触发器实现了相同的结果。这样我就不需要在触发器中编写任何插入语句。

ALTER TRIGGER "InsertIntoCardDetail" before insert on
DBA.CardDetail
referencing new as new_name
for each row
begin
  if(new_name.hourfinish is null)
    and exists(select 1
      from CardDetail
      where hourfinish is null) then
    raiserror 99999 'Cannot have Multiple Nulls'
  end if;
end
于 2012-10-02T14:50:15.867 回答