Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a sql stored procedure that am using to get all the table in my sql database am also excluding user membership and system diagram tables in my results that is working fine. my challenge is I want to create another stored procedure where I will take each table on the list on tables that I got when am running the first store procedure and create the audit trail for each so that I can see who was doing what, when. I want to have an audit trail table for this results with this fields
UserId, tablename, what change, datetime.

can you please assist

What I have tried:

this what I tried
USE [MISA]
GO
/****** Object:  StoredProcedure [dbo].[sp_ListAuditTables]    Script Date: 14/02/2018 14:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_ListAuditTables] 
	-- Add the parameters for the stored procedure here
	--<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	--<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here

--SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME NOT LIKE 'ASP%' AND TABLE_NAME NOT LIKE 'SYS%'
--ORDER BY TABLE_NAME 
	--SELECT 'exec [sp_GenerateAuditTrail] @TableName=''' + TABLE_NAME + '''' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'
	--ORDER BY TABLE_NAME

		SELECT 'exec [sp_GenerateAuditTrail] @TableName=''' + TABLE_NAME + '''' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME NOT LIKE 'ASP%' AND TABLE_NAME NOT LIKE 'SYS%'
	ORDER BY TABLE_NAME
END


with a [sp_GenerateAuditTrail]
Posted
Updated 14-Feb-18 1:54am
v2
Comments
Maciej Los 14-Feb-18 2:19am    
And what have you tried till now?
Note: we don't see your screen and have no access to your data. You have to be more specific and provide more details, if you want our help.

1 solution

You can't create an audit trail "after the event" - you have to capture the changes happening.

There are several techniques you can use - this article discusses some of them
Creating a successful auditing strategy for your SQL Server databases[^]

A technique we used on a (much) older version of SQL was to have triggers on the tables we wanted auditing that wrote "copy" records to another database.

I can't emphasise enough the importance of getting your strategy right first - this can easily run away with you if you just dive straight in.
 
Share this answer
 
Comments
Maciej Los 14-Feb-18 13:43pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900