SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


An Audit Trail Generator


An Audit Trail Generator

Author
Message
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14718 Visits: 9518
At a minimum you need to 1) script out the a_District table and 2) do as Ninja asked and add a "Print @cmd" to your p_trigger procedure right before the "Exec (@cmd)". Then post both of these things here.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Tech_Newbie
Tech_Newbie
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 119
My audit table (for history of District)
USE [JuryDB]
GO
/****** Object: Table [dbo].[a_District] Script Date: 04/17/2008 09:14:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[a_District](
[DistrictNum] [int] NOT NULL,
[Region] [varchar](50) NOT NULL,
[D_Addr1] [varchar](50) NOT NULL,
[D_Addr2] [varchar](50) NULL,
[D_City] [varchar](50) NOT NULL,
[D_Phn] [varchar](15) NULL,
[D_Tollfree] [varchar](15) NULL,
[D_Zip] [varchar](10) NOT NULL,
[D_JurPhn] [varchar](15) NOT NULL,
[D_State] [char](2) NOT NULL,
[Budget_Org] [varchar](50) NULL,
[Cost_Org] [varchar](50) NULL,
[Cost_Mile] [varchar](50) NULL,
[Attend_Fee] [varchar](50) NULL,
[row_id] [int] NULL,
[modid] [int] NOT NULL,
[audit_id] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK__a_District__28B808A7] PRIMARY KEY CLUSTERED
(
[audit_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


-----------------------------------------------------------------------------


AGAIN THE P_TRIGGER - THIS TIME I HAVE MARKED THE LINE IN WHICH THE TRANSACTION HALTS ON:

/***********************P_TRIGGER*************************************/




USE [JuryDB]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_trigger]
@tblname VARCHAR(128)
as
BEGIN
declare @cnti int, @cntd int, @maxid int, @minid int, @cmd varchar(4000), @audittblname varchar(128),
@cmdINSERT varchar(1000), @cmdSELECT varchar(1000), @modif_id bigint,
@cmdFROM varchar(255), @AUDITINSERT varchar(255), @DUI varchar(10)
set @audittblname = 'a_' + substring(@tblname,1,128)


declare @tmp table (cname varchar(128), cid int identity(1,1) )

select @cnti = count(*) from #ti

select @cntd = count(*) from #td

-- check how many rows changed. If 0 then do nothing

IF (@cnti = 0 and @cntd = 0)
return 0
-- get all table columns


insert into @tmp(cname)
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = @tblname

select @maxid = max(cid), @minid = min(cid) from @tmp

set @cmdSELECT = ' SELECT '

set @cmdINSERT = ' INSERT INTO ' + @audittblname + ' ( '

while (@minid <= @maxid)
begin
-- begin while
select @cmdINSERT = @cmdINSERT + cname + ',' , @cmdSELECT = @cmdSELECT + 'd.' + cname + ','
FROM @tmp
where cid = @minid
set @minid = @minid + 1
end
-- end while

-- always set new rows for the AUDIT

IF (@cnti = @cntd )
begin
set @DUI = ' ''U'' '
SET @cmdFROM = ' FROM #ti d '
end

IF (@cnti < @cntd)
begin
set @DUI = ' ''D'' '
SET @cmdFROM = ' FROM #td d '
end

------------------------ERRORS DISPLAY ON THIS LINE, OF COURSE THERE IS SOMETHING-------
------------------------FISHY HAPPENING BEFORE IT HITS THIS POINT-----------
------------------------DOES NOT GET PAST THIS POINT----------------



IF (@cnti > @cntd) PRINT 'HERE IS WHERE MY APPLICATION STOPS'
begin
set @DUI = ' ''I'' '
PRINT @DUI
SET @cmdFROM = ' FROM #ti d '
end

-- insert record into table modif
insert into modif(tablenm, dml_type)
select @tblname, @dui
PRINT @tblname
PRINT @dui
-- get identity
select @modif_id = SCOPE_IDENTITY( )
-- add modification column value
set @AUDITINSERT = ' modid )'

set @cmd = @cmdINSERT + @AUDITINSERT + @cmdSELECT + cast(@modif_id as varchar) + @cmdFROM

PRINT N'CMD: '+ @cmd+N'';


exec (@cmd)

SET NOCOUNT OFF

END

PRINT N'CMD2: '+ @cmd+N'';
GO
Tech_Newbie
Tech_Newbie
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 119
ALSO - YES, I WOULD BE HAPPY WITH A NON - DYNAMIC SQL SOLUTION

REQUIREMENTS

AFTER EACH UPDATE, INSERTION, OR DELETE

I NEED TO CAPTURE ALL INFO IN RECORD BEFORE CHANGE, ALL INFO AFTER CHANGE, THE TYPE OF CHANGE THAT OCCURRED, WHICH USER MADE THE CHANGE, THE TIME AND DATE OF THE CHANGE
jeffbrady
jeffbrady
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 5
The problem with the sp is that the value 'I' is being inserted into a field defined as char(1), so this sp would never work.

Change 'I' to ''I'' and it should work. This same change should be made for the other transaction types also.



IF (@cnti > @cntd)
BEGIN
set @DUI = ' ''I'' ' --should be set @DUI = 'I'
SET @cmdFROM = ' FROM #ti d '
END

-- insert record into table modif
insert into modif(tablenm, dml_type)
select @tblname, @dui
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search