[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

fhillipo

3/20/2007 11:17:00 AM

I have a requirement to create an audit trail for a set of SQL Server 2000
database tables. The tables have no primary keys.

The audit fields are

createdBy
dateCreated
UpdatedBy
dateUpdated

I successfully updated the table with the 4 fields for audit trail
The first 2 audit fields (createdBy, dateCreated) I've populated
successfully using Default [User_Name() and Getdate()]
To populated the UpdatedBy and DateUpdated fields I've opted for triggers.
However when it comes to the trigger part there is a slight problem.

The trigger fires to update the UpdatedBy and dateUpdated fields for all
records that existed before the 4 audit trail fields were added.
However, for newly inserted records, the updatedBy and dateUpdated fields
were not updated because the trigger failed to fire.

Any assistance appreciated.

______________________________________________

Please find below the table update and trigger definition:

--Table Update

ALTER TABLE [dbo].[Table_1] ADD
[createdBy] [varchar] (40) DEFAULT USER_NAME() NOT NULL,
[dateCreated] [datetime] DEFAULT GETDATE() NOT NULL,
[updatedBy] [varchar] (40),
[dateUpdated] [datetime] NULL

--Trigger Definition

CREATE TRIGGER [TR_Table_1]
ON Table_1
FOR UPDATE
AS
IF EXISTS (SELECT d.col_a, d.col_b, d.col_c, d.col_d, d.col_e FROM deleted
d, Table_1)
--IF (SELECT COUNT(*) FROM deleted, Table_1) > 0
BEGIN
Update Database_1..Table_1
SET updatedBy = USER_NAME(),
dateUpdated = GETDATE()
WHERE col_a IN (SELECT deleted.col_a FROM deleted, Table_1)
AND col_b IN (SELECT deleted.col_b FROM deleted, Table_1)
AND col_c IN (SELECT deleted.col_c FROM deleted, Table_1)
AND col_d IN (SELECT deleted.col_d FROM deleted, Table_1)
AND col_e IN (SELECT deleted.col_e FROM deleted, Table_1)
END

fhillipo
48 Answers

TheSQLGuru

3/20/2007 1:40:00 PM

0

The trigger failed to fire for INSERTs because it is only declared FOR
UPDATE. Triggers can fire for any combination of UPDATE, DELETE and INSERT.

--
TheSQLGuru
President
Indicium Resources, Inc.

"fhillipo" <fhillipo@discussions.microsoft.com> wrote in message
news:194946EB-FEC7-4D35-B6D1-B6D7E81BAEA5@microsoft.com...
>I have a requirement to create an audit trail for a set of SQL Server 2000
> database tables. The tables have no primary keys.
>
> The audit fields are
>
> createdBy
> dateCreated
> UpdatedBy
> dateUpdated
>
> I successfully updated the table with the 4 fields for audit trail
> The first 2 audit fields (createdBy, dateCreated) I've populated
> successfully using Default [User_Name() and Getdate()]
> To populated the UpdatedBy and DateUpdated fields I've opted for triggers.
> However when it comes to the trigger part there is a slight problem.
>
> The trigger fires to update the UpdatedBy and dateUpdated fields for all
> records that existed before the 4 audit trail fields were added.
> However, for newly inserted records, the updatedBy and dateUpdated fields
> were not updated because the trigger failed to fire.
>
> Any assistance appreciated.
>
> ______________________________________________
>
> Please find below the table update and trigger definition:
>
> --Table Update
>
> ALTER TABLE [dbo].[Table_1] ADD
> [createdBy] [varchar] (40) DEFAULT USER_NAME() NOT NULL,
> [dateCreated] [datetime] DEFAULT GETDATE() NOT NULL,
> [updatedBy] [varchar] (40),
> [dateUpdated] [datetime] NULL
>
> --Trigger Definition
>
> CREATE TRIGGER [TR_Table_1]
> ON Table_1
> FOR UPDATE
> AS
> IF EXISTS (SELECT d.col_a, d.col_b, d.col_c, d.col_d, d.col_e FROM deleted
> d, Table_1)
> --IF (SELECT COUNT(*) FROM deleted, Table_1) > 0
> BEGIN
> Update Database_1..Table_1
> SET updatedBy = USER_NAME(),
> dateUpdated = GETDATE()
> WHERE col_a IN (SELECT deleted.col_a FROM deleted, Table_1)
> AND col_b IN (SELECT deleted.col_b FROM deleted, Table_1)
> AND col_c IN (SELECT deleted.col_c FROM deleted, Table_1)
> AND col_d IN (SELECT deleted.col_d FROM deleted, Table_1)
> AND col_e IN (SELECT deleted.col_e FROM deleted, Table_1)
> END
>
> fhillipo


Hugo Kornelis

3/20/2007 6:39:00 PM

0

On Tue, 20 Mar 2007 04:17:00 -0700, fhillipo wrote:

>I have a requirement to create an audit trail for a set of SQL Server 2000
>database tables. The tables have no primary keys.

Hi fhillipo,

There's the bottom line of your problem. If there's no pprimary key, you
can (and eventually WILL) have two or more rows with the exact same
content. Then, there is no way to address just one of them anymore.

(snip)
>CREATE TRIGGER [TR_Table_1]
>ON Table_1
>FOR UPDATE
>AS
>IF EXISTS (SELECT d.col_a, d.col_b, d.col_c, d.col_d, d.col_e FROM deleted
>d, Table_1)
>--IF (SELECT COUNT(*) FROM deleted, Table_1) > 0
>BEGIN
> Update Database_1..Table_1
> SET updatedBy = USER_NAME(),
> dateUpdated = GETDATE()
> WHERE col_a IN (SELECT deleted.col_a FROM deleted, Table_1)
> AND col_b IN (SELECT deleted.col_b FROM deleted, Table_1)
> AND col_c IN (SELECT deleted.col_c FROM deleted, Table_1)
> AND col_d IN (SELECT deleted.col_d FROM deleted, Table_1)
> AND col_e IN (SELECT deleted.col_e FROM deleted, Table_1)
>END

You can improve this somewhat by changing it to

CREATE TRIGGER [TR_Table_1]
ON Table_1
FOR UPDATE
AS
-- Performance: bail out immediately if no rows were effected
IF @@ROWCOUNT = 0 RETURN

-- Suppress "xxx row(s) affected" messages from trigger
SET ROWCOUNT OFF

-- Mark changed rows
UPDATE Table_1
SET updatedBy = USER_NAME(),
dateUpdate = CURRENT_TIMESTAMP
WHERE EXISTS
(SELECT *
FROM inserted AS i
WHERE i.col_a = Table_1.col_a
AND i.col_b = Table_1.col_b
AND i.col_c = Table_1.col_c
AND i.col_d = Table_1.col_d
AND i.col_e = Table_1.col_e)
GO

Note that this will STILL mark ALL rows with the same values for the
five columns in the subquery even if only one was updated. There is no
way to prevent that without a primary key on the table.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hug...

fhillipo

3/28/2007 12:52:00 AM

0

Hugo,

Thanks a million. This worked beautifully!

--
fhillipo


"Hugo Kornelis" wrote:

> On Tue, 20 Mar 2007 04:17:00 -0700, fhillipo wrote:
>
> >I have a requirement to create an audit trail for a set of SQL Server 2000
> >database tables. The tables have no primary keys.
>
> Hi fhillipo,
>
> There's the bottom line of your problem. If there's no pprimary key, you
> can (and eventually WILL) have two or more rows with the exact same
> content. Then, there is no way to address just one of them anymore.
>
> (snip)
> >CREATE TRIGGER [TR_Table_1]
> >ON Table_1
> >FOR UPDATE
> >AS
> >IF EXISTS (SELECT d.col_a, d.col_b, d.col_c, d.col_d, d.col_e FROM deleted
> >d, Table_1)
> >--IF (SELECT COUNT(*) FROM deleted, Table_1) > 0
> >BEGIN
> > Update Database_1..Table_1
> > SET updatedBy = USER_NAME(),
> > dateUpdated = GETDATE()
> > WHERE col_a IN (SELECT deleted.col_a FROM deleted, Table_1)
> > AND col_b IN (SELECT deleted.col_b FROM deleted, Table_1)
> > AND col_c IN (SELECT deleted.col_c FROM deleted, Table_1)
> > AND col_d IN (SELECT deleted.col_d FROM deleted, Table_1)
> > AND col_e IN (SELECT deleted.col_e FROM deleted, Table_1)
> >END
>
> You can improve this somewhat by changing it to
>
> CREATE TRIGGER [TR_Table_1]
> ON Table_1
> FOR UPDATE
> AS
> -- Performance: bail out immediately if no rows were effected
> IF @@ROWCOUNT = 0 RETURN
>
> -- Suppress "xxx row(s) affected" messages from trigger
> SET ROWCOUNT OFF
>
> -- Mark changed rows
> UPDATE Table_1
> SET updatedBy = USER_NAME(),
> dateUpdate = CURRENT_TIMESTAMP
> WHERE EXISTS
> (SELECT *
> FROM inserted AS i
> WHERE i.col_a = Table_1.col_a
> AND i.col_b = Table_1.col_b
> AND i.col_c = Table_1.col_c
> AND i.col_d = Table_1.col_d
> AND i.col_e = Table_1.col_e)
> GO
>
> Note that this will STILL mark ALL rows with the same values for the
> five columns in the subquery even if only one was updated. There is no
> way to prevent that without a primary key on the table.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hug...
>

duke

2/7/2013 1:04:00 PM

0

On Wed, 06 Feb 2013 23:53:14 -0800, Jeanne Douglas <hlwdjsd2@NOSPAMgmail.com>
wrote:

>In article <kpg6h8p9vvbesvev397fv2bipenl0h0epe@4ax.com>,
> J <jdyoung1@ymail.com> wrote:
>
>> She was a wise and wonderful woman and we have all been graced that
>> this previousl unknown interview has been uncovered.

>She was a hypocritical vicious sociopathic monster. She refused to help
>ease the pain of her patients, telling them it was holy to suffer, but
>when she got sick, it was off to the best doctors and hospitals for her.

She is now BLESSED Mother Theresa. Next stop is sainthood. If a book suits
your fancy, you'll blindly bite on anything.

The dukester, American - American
********************************************
You can't fix stupid.
********************************************

duke

2/7/2013 1:06:00 PM

0

On Thu, 07 Feb 2013 02:49:15 -0800, r@somis.org (?RLMeasures) wrote:

>In article <hlwdjsd2-490E8B.23531406022013@news.giganews.com>, Jeanne
>Douglas <hlwdjsd2@NOSPAMgmail.com> wrote:
>
>> In article <kpg6h8p9vvbesvev397fv2bipenl0h0epe@4ax.com>,
>> J <jdyoung1@ymail.com> wrote:
>>
>> > She was a wise and wonderful woman and we have all been graced that
>> > this previousl unknown interview has been uncovered.
>>
>> She was a hypocritical vicious sociopathic monster. She refused to help
>> ease the pain of her patients, telling them it was holy to suffer, but
>> when she got sick, it was off to the best doctors and hospitals for her.
>
>? Jeanne - the problem was that she believed the RCC's teaching that
>uffering is one of the paths to RC Heaven.

Only if offered for others to come to God.

The dukester, American - American
********************************************
You can't fix stupid.
********************************************

W.T.S.

2/7/2013 2:13:00 PM

0

In article <jf97h8tne9e58ilvih6icsccjl5r10npl0@4ax.com>, duckgumbo32
@cox.net says...
>
> On Wed, 06 Feb 2013 23:53:14 -0800, Jeanne Douglas <hlwdjsd2@NOSPAMgmail.com>
> wrote:
>
> >In article <kpg6h8p9vvbesvev397fv2bipenl0h0epe@4ax.com>,
> > J <jdyoung1@ymail.com> wrote:
> >
> >> She was a wise and wonderful woman and we have all been graced that
> >> this previousl unknown interview has been uncovered.
>
> >She was a hypocritical vicious sociopathic monster. She refused to help
> >ease the pain of her patients, telling them it was holy to suffer, but
> >when she got sick, it was off to the best doctors and hospitals for her.
>
> She is now BLESSED Mother Theresa. Next stop is sainthood. If a book suits
> your fancy, you'll blindly bite on anything.
So, the RCC awards Sainthood to anyone who is sadistic enough. So, what
else is new?
>

Abortion and sterilization, they save the lives, health and futures of
women and men alike!
>
http://www.rhrealitycheck.org/p...
>
http://www.jennyje...
>
http://tinyurl.c...
>
http://tinyurl.c...
>
http://tinyurl.c...
>
http://www.egalitarian.biz/Plan-B--Remedy-of-a-Lif...
>
http://www.thelizlibrary.org/l...
>
Breed like rabbits, live like pigs, die like rats!
>
Modern Christian: Someone who can take time out from
complaining about "welfare mothers popping out babies we
have to feed" to complain about welfare mothers getting
abortions that PREVENT more babies to be raised at public
expense.
>
http://www.imno...
>
http://img.gawkerassets.com/img/17gbnyv0yzhevjpg/or...
>
http://tinyurl.c...
>
http://tinyurl.c...
>
http://tinyurl.c...
>
http://tinyurl.c...
>
http://tinyurl.c...

Syd M.

2/7/2013 2:35:00 PM

0

On Feb 7, 5:49 am, r...@somis.org (•RLMeasures) wrote:
> In article <hlwdjsd2-490E8B.23531406022...@news.giganews.com>, Jeanne
>
> Douglas <hlwdj...@NOSPAMgmail.com> wrote:
> > In article <kpg6h8p9vvbesvev397fv2bipenl0h0...@4ax.com>,
> >  J <jdyou...@ymail.com> wrote:
>
> > > She was a wise and wonderful woman and we have all been graced that
> > > this previousl unknown interview has been uncovered.
>
> > She was a hypocritical vicious sociopathic monster. She refused to help
> > ease the pain of her patients, telling them it was holy to suffer, but
> > when she got sick, it was off to the best doctors and hospitals for her.
>
> •  Jeanne - the problem was that she believed the RCC's teaching that
> uffering is one of the paths to RC Heaven.

Except when her own life was on the line, then it's only the best
hospitals for her, the selfish, sadistic old bitch.

PDW

Syd M.

2/7/2013 2:39:00 PM

0

On Feb 7, 8:04 am, duke <duckgumb...@cox.net> wrote:
> On Wed, 06 Feb 2013 23:53:14 -0800, Jeanne Douglas <hlwdj...@NOSPAMgmail.com>
> wrote:
>
> >In article <kpg6h8p9vvbesvev397fv2bipenl0h0...@4ax.com>,
> > J <jdyou...@ymail.com> wrote:
>
> >> She was a wise and wonderful woman and we have all been graced that
> >> this previousl unknown interview has been uncovered.
> >She was a hypocritical vicious sociopathic monster. She refused to help
> >ease the pain of her patients, telling them it was holy to suffer, but
> >when she got sick, it was off to the best doctors and hospitals for her.
>
> She is now BLESSED Mother Theresa.  Next stop is sainthood.  If a book suits
> your fancy, you'll blindly bite on anything.
>
>

Figures you'd make a saint out of that sadistic old bat.
You kind are sadistic busybodies to a all.

PDW

Syd M.

2/7/2013 2:40:00 PM

0

On Feb 7, 8:05 am, duke <duckgumb...@cox.net> wrote:
> On Thu, 07 Feb 2013 02:49:15 -0800, r...@somis.org (•RLMeasures) wrote:
> >In article <hlwdjsd2-490E8B.23531406022...@news.giganews.com>, Jeanne
> >Douglas <hlwdj...@NOSPAMgmail.com> wrote:
>
> >> In article <kpg6h8p9vvbesvev397fv2bipenl0h0...@4ax.com>,
> >>  J <jdyou...@ymail.com> wrote:
>
> >> > She was a wise and wonderful woman and we have all been graced that
> >> > this previousl unknown interview has been uncovered.
>
> >> She was a hypocritical vicious sociopathic monster. She refused to help
> >> ease the pain of her patients, telling them it was holy to suffer, but
> >> when she got sick, it was off to the best doctors and hospitals for her.
>
> >•  Jeanne - the problem was that she believed the RCC's teaching that
> >uffering is one of the paths to RC Heaven.
>
> Only if offered for others to come to God.
>
>

No.
PROVE this 'god' exists, liar Earl.

PDW

gazelle

2/7/2013 2:45:00 PM

0

In article <b1065122-55d1-4f44-9a1b-bf023469d01a@j6g2000yqj.googlegroups.com>,
Syd M. <pauldavidwright@yahoo.com> wrote:
....
>> ? ?Jeanne - the problem was that she believed the RCC's teaching that
>> uffering is one of the paths to RC Heaven.
>
>Except when her own life was on the line, then it's only the best
>hospitals for her, the selfish, sadistic old bitch.

Suppose you actually believe that MT was great, wonderful, and special.

Yes, I know that's asking a lot, but bear with me.

Now, the logic goes like this:

1) You, the poor ordinary everyman, you get a pass into heaven b/c
suffering is good. Bully for you!

2) But me? I'm special. I've got important work to do here on earth.
Therefore, my life must be preserved, by any and all means necessary
and possible.

See, not too difficult, really...

--
"Every time Mitt opens his mouth, a swing state gets its wings."

(Should be on a bumper sticker)