Re: audit trigger
From: Darren Woodbrey (darrenwoodbrey_at_hpfairfield.com)
Date: 06/07/04
- Next message: Joe Celko: "Re: mueric representation of a string"
- Previous message: Aaron [SQL Server MVP]: "Re: Date Format"
- In reply to: John Bell: "Re: audit trigger"
- Next in thread: Aaron [SQL Server MVP]: "Re: audit trigger"
- Reply: Aaron [SQL Server MVP]: "Re: audit trigger"
- Reply: John Bell: "Re: audit trigger"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 7 Jun 2004 16:37:02 -0400
What do you mean by compound key? I am getting a primary key error. How do
I initialize @PKCols? I am new to SQL and have found this code from the
web. Sorry for the lack of knowledge. Thanks!
"John Bell" <jbellnewsposts@hotmail.com> wrote in message
news:OLLZdsMTEHA.3844@TK2MSFTNGP11.phx.gbl...
> Hi
>
> Your procedure will not work when you get a compound key. I am not sure
why
> you have the temporary tables direct use of the inserted/deleted "tables"
> should be ok! There is no DDL for the audit table and even if it did work
> you may get primary key problems. As @PKCols is not initialised it will
> always be NULL. The method you use to create the value of @PKCols is not
> regarded as safe and the best way is usually a cursor. There is alot of
work
> in this trigger which could be reduced and speeded up by having table
> specific code even through maintainance will be a nightmare. You may also
> find that the audit table is a bottleneck.
>
> John
>
>
> "Darren Woodbrey" <darrenwoodbrey@hpfairfield.com> wrote in message
> news:O2ZQJYMTEHA.3988@tk2msftngp13.phx.gbl...
> > I am trying to implement an audit trigger I found on the net. It gives
me
> > an error when I try to update a record saying "no PK on table wo_main".
> The
> > table wo_main does have a primary key 'entry'. Can anyone help me out
> with
> > this? Here is the trigger:
> >
> > create trigger tr_wo_main on wo_main for update
> > as
> > declare @bit int ,
> > @field int ,
> > @maxfield int ,
> > @char int ,
> > @fieldname varchar(128) ,
> > @TableName varchar(128) ,
> > @PKCols varchar(1000) ,
> > @sql varchar(2000),
> > @UpdateDate varchar(21) ,
> > @UserName varchar(128)
> >
> > select @TableName = 'wo_main'
> > -- date and user
> > select @UserName = system_user ,
> > @UpdateDate = convert(varchar(8), getdate(), 112) + ' ' +
> > convert(varchar(12), getdate(), 114)
> > -- get list of columns
> > select * into #ins from inserted
> > select * into #del from deleted
> >
> > -- Get primary key columns for full outer join
> > select @PKCols = coalesce(@PKCols + ' and', ' on') + ' i.' +
> c.COLUMN_NAME
> > + ' = d.' + c.COLUMN_NAME
> > from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
> > INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
> > where pk.TABLE_NAME = @TableName
> > and CONSTRAINT_TYPE = 'PRIMARY KEY'
> > and c.TABLE_NAME = pk.TABLE_NAME
> > and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
> >
> > if @PKCols is null
> > begin
> > raiserror('no PK on table %s', 16, -1, @TableName)
> > return
> > end
> >
> > select @field = 0, @maxfield = max(ORDINAL_POSITION) from
> > INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @TableName
> > while @field < @maxfield
> > begin
> > select @field = min(ORDINAL_POSITION) from INFORMATION_SCHEMA.COLUMNS
> > where TABLE_NAME = @TableName and ORDINAL_POSITION > @field
> > select @bit = (@field - 1 )% 8 + 1
> > select @bit = power(2,@bit - 1)
> > select @char = ((@field - 1) / 8) + 1
> > if substring(COLUMNS_UPDATED(),@char, 1) & @bit > 0
> > begin
> > select @fieldname = COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where
> > TABLE_NAME = @TableName and ORDINAL_POSITION = @field
> > select @sql = 'insert Audit (TableName, FieldName, OldValue,
> NewValue,
> > UpdateDate, UserName)'
> > select @sql = @sql + ' select ''' + @TableName + ''''
> > select @sql = @sql + ',''' + @fieldname + ''''
> > select @sql = @sql + ',convert(varchar(1000),d.' + @fieldname + ')'
> > select @sql = @sql + ',convert(varchar(1000),i.' + @fieldname + ')'
> > select @sql = @sql + ',''' + @UpdateDate + ''''
> > select @sql = @sql + ',''' + @UserName + ''''
> > select @sql = @sql + ' from #ins i full outer join #del d'
> > select @sql = @sql + @PKCols
> > select @sql = @sql + ' where i.' + @fieldname + ' <> d.' +
@fieldname
> > select @sql = @sql + ' or (i.' + @fieldname + ' is null and d.' +
> > @fieldname + ' is not null)'
> > select @sql = @sql + ' or (i.' + @fieldname + ' is not null and d.'
+
> > @fieldname + ' is null)'
> >
> > exec (@sql)
> > end
> > end
> > go
> >
> >
> > Thanks in advance for any help you can give.
> > Darren
> > MCP
> >
> >
>
>
- Next message: Joe Celko: "Re: mueric representation of a string"
- Previous message: Aaron [SQL Server MVP]: "Re: Date Format"
- In reply to: John Bell: "Re: audit trigger"
- Next in thread: Aaron [SQL Server MVP]: "Re: audit trigger"
- Reply: Aaron [SQL Server MVP]: "Re: audit trigger"
- Reply: John Bell: "Re: audit trigger"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|