保存对审核跟踪表的更改的Sql触发器
我开始使用ADSSQL表触发器来存储对一个特定表所做的更改。以下是想法:保存对审核跟踪表的更改的Sql触发器,sql,triggers,advantage-database-server,Sql,Triggers,Advantage Database Server,我开始使用ADSSQL表触发器来存储对一个特定表所做的更改。以下是想法: //-------- sql trigger to store changes on patients table to auditLog Table //---------------------------------------------------------------------- declare cChanges Char( 5000 ); declare allColumns Cursor ; decla
//-------- sql trigger to store changes on patients table to auditLog Table
//----------------------------------------------------------------------
declare cChanges Char( 5000 );
declare allColumns Cursor ;
declare FieldName Char( 25 );
declare StrSql Char( 255 );
declare @new cursor as select * from __new;
declare @old cursor as select * from __old;
open @old;
fetch @old;
open @new;
fetch @new;
Set cChanges = '';
Open AllColumns as Select * from system.columns where parent = 'patients';
while fetch allColumns DO
// Try
FieldName = allColumns.Name;
StrSql = 'IF @new.'+FieldName
+ '<> @old.'+FieldName
+' and @old.'+FieldName + '<> [ ] THEN '
+ 'cChanges = Trim( '+cChanges+' ) + @old.'+FieldName
+ ' Changed to ' + '@new.'+fieldname
+ ' | '+ 'ENDIF ; ' ;
Execute Immediate StrSql ;
// Catch ALL
// End Try;
End While;
if cChanges <> '' THEN
Insert Into AuditLog ( TableKey, Patient, [table], [user], creation, Changes )
values( @new.patient, @new.patient, [Patietns], User(), Now(), cChanges ) ;
ENDIF;
CLOSE AllColumns;
//--------------------------
报告变量cChanges的上述触发代码错误不存在
有人能帮忙吗
Reinaldo。我认为问题与动态SQl试图设置触发器主体中声明的值有关 e、 g.您的cChanges=TRIM语句可能导致问题,因为该上下文中不存在cChanges 您应该使用绑定变量来实现这一点,而不是尝试使用=符号进行设置 你可以在他们的文档中看到,他们说你不能直接访问这些变量
问题确实是,您无法访问脚本中的局部变量并立即执行。要解决此问题,您可以使用临时表:
//-------- sql trigger to store changes on patients table to auditLog Table
//----------------------------------------------------------------------
declare cChanges Char( 5000 );
declare allColumns Cursor ;
declare FieldName Char( 25 );
declare StrSql Char( 255 );
Set cChanges = '';
Open AllColumns as Select * from system.columns where parent = 'patients';
while fetch allColumns DO
// Try
FieldName = allColumns.Name;
StrSql = 'SELECT n.FieldName newVal,'
+ 'o.FieldName oldVal '
+ 'INTO #MyTrigTable '
+ 'FROM __new n, __old o';
EXECUTE IMMEDIATE strSQL;
IF ( SELECT oldVal FROM #myTrigTable ) <> '' THEN
IF ( SELECT newVal FROM #myTrigTable ) <> ( SELECT oldVal FROM #myTrigTable ) THEN
cChanges = 'Construct_SomeThing_Using_#myTrigTable_or_a_cursorBasedOn#MyTrigTable';
INSERT INTO AuditLog ( TableKey, Patient, [table], [user], creation, Changes )
SELECT patient, patient, 'Patietns', User(), Now(), cChages FROM __new ;
END;
END;
DROP TABLE #myTrigTable;
// Catch ALL
// End Try;
End While;
CLOSE AllColumns;
//--------------------------
可能不相关,但是值@new.patient、@new.patient、[Patietns],用户。。。打字错误[病人]?