473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MS SQL 8, track changes?

Plater
7,872 Expert 4TB
As a quick setup:
My company uses MS SQL SERVER 8 as a backend, and uses an old mdb/mde file as a frontend with linked tables.

Lately we've been running into a problem where a particular entry in a table is getting deleted. And nobody knows why. The table does not get additions or subtractions to it. Its used as shipping choices for another table.
Like:
1 Priority
2 NextDay
3 OverNight
(etc)

And the exact same entry keeps getting removed. We have been unable to find out where/why its happening.

I was wondering if there was a way to configure the SQL server to track anything it could about changes made to that table?
Such as a snapshot of the user instance like its displayed in the "Activity Monitor" (SQL Server Management Studio)?
Really I think a datetime and the computer/user of the connection that causes the change would be good.

Any ideas? I cannot find any reporting system to use for this.
Dec 10 '08 #1
6 3542
ck9663
2,878 Expert 2GB
If the table is in SQL Server, you can create a trigger to get all the information (ie system_user(), date and time) when someone delete it.

-- CK
Dec 12 '08 #2
Plater
7,872 Expert 4TB
I found the triggers after some digging, am unfamiliar with system_user(), I hope it does what I think it does.
thanks!


edit: its system_user, not system_user() it would seem
Dec 12 '08 #3
ck9663
2,878 Expert 2GB
Yes. It's a function that returns the current user name and is very helpful specially if your server is configured to accept Mix Authentication. What you do is to create a trigger that insert to a "tracking" table the date, tmie and the user name who did the change. Since the user does not know there's a trigger on the table, you can actually monitor the movement of your data.whether INSERT, UPDATE or DELETE.

Happy coding!
Dec 12 '08 #4
Plater
7,872 Expert 4TB
Yes indeed, here's what I ended up going with.

There is probably a more effecient way to do this, but the hope is that it never gets triggered

Expand|Select|Wrap|Line Numbers
  1. -- Insert statements for trigger here
  2. DECLARE @WhatType as varchar(20)
  3. DECLARE @WhoDidIt as varchar(100)
  4. DECLARE @Notes as varchar(500)
  5. DECLARE @Check1 as int
  6. DECLARE @Check2 as int
  7.  
  8. SET @WhoDidIt = system_user
  9. SET @Notes='Testing'
  10.  
  11. SELECT @Check1 = (SELECT COUNT(*) FROM Inserted)
  12. SELECT @Check2 = (SELECT COUNT(*) FROM Deleted)
  13.  
  14. if(@Check1>0 AND @Check2>0)
  15. BEGIN
  16.     --isUpdate
  17.     Set @WhatType='Update'
  18.     SELECT @Notes='Delete: '+(SELECT SomeColumn FROM Deleted)
  19.     Set @Notes=@Notes+ 'Insert: '+(SELECT SomeColumn FROM Inserted)
  20. END
  21. ELSE
  22.     if(@Check1>0 AND @Check2=0)
  23.     BEGIN
  24.         --isInsert
  25.         Set @WhatType='Insert'
  26.         SELECT @Notes='Insert: '+(SELECT SomeColumn FROM Inserted)
  27.     END
  28.     ELSE
  29.         if(@Check1=0 AND @Check2>0)
  30.         BEGIN
  31.             --isDelete
  32.             Set @WhatType='Delete'
  33.             SELECT @Notes='Delete: '+(SELECT SomeColumn FROM Deleted)
  34.         END
  35.         ELSE
  36.             BEGIN
  37.                 Set @WhatType='Unknown'
  38.             END
  39.  
  40. INSERT INTO [ChangesTracking]
  41. (
  42.     [Occured]
  43.    ,[WhoDidIt]
  44.    ,[TableAffected]
  45.    ,[Type]
  46.    ,[Notes]
  47. )
  48. VALUES
  49.  (
  50.     getdate()
  51.    ,@WhoDidIt
  52.    ,'MyTableName'
  53.    ,@WhatType
  54.    ,@Notes
  55. )
  56.  
Dec 15 '08 #5
ck9663
2,878 Expert 2GB
Don't forget error handling and if you can use transaction, use it.

-- CK
Dec 15 '08 #6
ck9663
2,878 Expert 2GB
Here's more about TRANSACTIONS.

-- CK
Dec 15 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: LC | last post by:
hi, i worry about people doing something they shouldn't to my db and I would like to track any structural changes (who and which)to my db. I am using oracle 8.0.6.0.0 and 9.2.0.2.0. regards,...
1
by: JLuppens | last post by:
Does anyone know if there is a way to track changes in a text field like word does? I am using a SQL2000 database and can use either VB.Net or Access. The field is either ntext or Varchar. I...
4
by: SQLDBA | last post by:
What would be the best practice to follow to keep track of MS SQL server changes... Stroed procs, tables, views, triggers, indexes, DTS and also jobs ect.... I am not quite sure how Source safe...
4
by: Nicole | last post by:
Hello: I have a shared database running on a network drive. I would like to track when record changes occur and who did it. Problem 1: I'm not able to tack changes, when they occur in...
2
by: Uwe Range | last post by:
Hi to all, I am developing a database where, with time, it proved to be necessary to keep track of the changes to the data. It is fairly easy to keep track of the time when a record was changed...
5
by: Daniel Walzenbach | last post by:
Hi, I need to track all changes made to an object. Consider the following class: Public Class Dog
2
by: metaperl | last post by:
I'm actually taking Microsoft's 2779 and just finished a lab where we kept track of our changes to the database. However, I'm not happy with the scripts interface because it does not tell me the...
3
by: PJ6 | last post by:
What's the best way to, in the general case, track changes to an object's public fields and properties? In a lot of applicatoins I've seen bahavior consistant with simple periodic checking from a...
20
by: John Sheppard | last post by:
Hello there, Does anyone know of a way to track changes to an SQL Server database so that I can easily run those changes at a later date? That is, I want to make schema changes, and record...
3
by: dirksza2009 | last post by:
Hi, I've made a multi user (4 end users) database in Access 2000. I've made data tables, reference tables etc which sits on a shared drive and I've made individual front ends for the end users...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.