Sybase 업데이트 트리거 - 업데이트를 위해 여러 행을 확인합니다

StackOverflow https://stackoverflow.com/questions/9502651

  •  14-11-2019
  •  | 
  •  

문제

업데이트를 시작하고 업데이트 전후 값을 비교하는 Sybase ASE 데이터베이스에 트리거를 작성하고 있습니다.업데이트가 실제로 데이터를 변경했는지 확인하려면이 작업을 수행합니다.이 경우 모니터링 테이블에 행을 추가합니다.불행히도 업데이트 명령이 1 행에만 영향을 미치는 경우에만 트리거 만 작동합니다.둘 이상의 행에 영향을 미치는 경우이 오류가 발생했습니다 :

Msg 512, Level 16, State 5:
Server 'myserver', Procedure 'myproc', Line 2:
Subquery returned more than 1 value.  This is illegal when the subquery follows =, !=, <, <= , >, >=, or when the subquery is used as an expression.
.

내 proc이 다음과 같습니다 :

create trigger mytrigger on mytable for update as

set nocount on

/* now do the insert if colum was updated  */
if update(col_a) and (select col_a from inserted) not in (select col_a from deleted) 
begin
    insert into monitoring_table (login,tablename,field,action,pstamp) 
        select suser_name(),'mytable','col_a','U',getdate() from inserted
end
/* now do the insert if colum was updated  */
if update(col_b) and (select col_b from inserted) not in (select col_b from deleted) 
begin
    insert into monitoring_table (login,tablename,field,action,pstamp) 
        select suser_name(),'mytable','col_b','U',getdate() from inserted
end
go
.

내 방아쇠 에서이 다중 업데이트 문제를 어떻게 둘러 볼 수 있는지 알 수 있습니까?

도움이 되었습니까?

해결책

오류가 트리거 된 경우 다음 작업은 다음과 같아야합니다. 대신 :

if update(col_a) and (select col_a from inserted) not in (select col_a from deleted)
.

사용

if update(col_a) and exists (select col_a from inserted where col_a not in (select col_a from deleted))
.

다른 팁

이렇게해야합니다 :

create trigger mytrigger on mytable for update as

set nocount on

/* now do the insert if colum was updated  */
if update(col_a) and (select col_a from inserted where col_a not in (select col_a from deleted) 
begin
    insert into monitoring_table (login,tablename,field,action,pstamp) 
        select suser_name(),'mytable','col_a','U',getdate() from inserted
end
/* now do the insert if colum was updated  */
if update(col_b) and (select col_b from inserted where col_b not in (select col_b from deleted) 
begin
    insert into monitoring_table (login,tablename,field,action,pstamp) 
        select suser_name(),'mytable','col_b','U',getdate() from inserted
end
go
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top