Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345

An Audit Trail Generator Expand / Collapse
Author
Message
Posted Wednesday, April 16, 2008 7:29 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #486063
Posted Thursday, April 17, 2008 9:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, March 6, 2010 11:15 AM
Points: 25, 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



Post #486545
Posted Thursday, April 17, 2008 9:22 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, March 6, 2010 11:15 AM
Points: 25, 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
Post #486547
Posted Sunday, June 8, 2008 4:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, June 8, 2008 4:06 PM
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
Post #513475
« Prev Topic | Next Topic »

Add to briefcase «««12345

Permissions Expand / Collapse