473,378 Members | 1,162 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,378 software developers and data experts.

Triggers in SQL 2005

1
I have a table(ACCOUNT) which has 30+ columns. There can be Inserts/Updates/Deletes to this table. My requirement is to create triggers for Insert/Update/Delete and store previous value and latest value for only the affected fields into ACCOUNT_AUDIT table

Example for ACCOUNT table

Field1 Field2 Field3 Field4 Field5
a b c d e ---->Old data

Field1 Field2 Field3 Field4 Field5
z b c y e ---->modified data


ACCOUNT_AUDIT table has 3 fields
Action,PreviousValue,NewValue,LastChangeDate

After applying the trigger on ACCOUNT table, ACCOUNT_AUDIT table should populate the data as follows.

Action: Update

PreviousValue:<Previous><Field1>a<Field1><Field4>d </Field4></Previous>

NewValue:<New><Field1>z<Field1><Field4>y</Field4></New>

LastChangeDate: 01/30/08 07:30:53.000


Can any one please help me to write trigger like this?
Many thanks in advance.
Jan 30 '08 #1
1 1035
ck9663
2,878 Expert 2GB
I have a table(ACCOUNT) which has 30+ columns. There can be Inserts/Updates/Deletes to this table. My requirement is to create triggers for Insert/Update/Delete and store previous value and latest value for only the affected fields into ACCOUNT_AUDIT table

Example for ACCOUNT table

Field1 Field2 Field3 Field4 Field5
a b c d e ---->Old data

Field1 Field2 Field3 Field4 Field5
z b c y e ---->modified data


ACCOUNT_AUDIT table has 3 fields
Action,PreviousValue,NewValue,LastChangeDate

After applying the trigger on ACCOUNT table, ACCOUNT_AUDIT table should populate the data as follows.

Action: Update

PreviousValue:<Previous><Field1>a<Field1><Field4>d </Field4></Previous>

NewValue:<New><Field1>z<Field1><Field4>y</Field4></New>

LastChangeDate: 01/30/08 07:30:53.000


Can any one please help me to write trigger like this?
Many thanks in advance.
pseudo-code:

1. create your trigger
2. have your "audit table" ready. this table should have the same structure as your transaction table with an extra field to store the string "Previous Value" and "New Value".
3. when your trigger fire, you can check if the field you want to monitor is the one updated or you don't have to and let the trigger fire regardless of the column updated....
4. for insert, do you need to create an audit record? if yes, insert the entire value of inserted table adding the "New Value" string to go to your extra field.
5. for deleted, do you need to create an audit record? if yes, insert the entire value of delete dtable adding the "Previous Value" string to go to your extra field.
6. for update, insert the entire deleted table adding the "Previous Value" to your extra string and then add the inserted table adding the "New Value" to your extra string...

again, this is pseudo-code...sorry, i don't spoon-feed ;) i just point to the a possible solution...if you hit a wall, post what you have so far, and we can debug your code for you...

-- ck

-- ck
Jan 30 '08 #2

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

Similar topics

4
by: stacdab | last post by:
We have a partitioned view with 4 underlying tables. The view and each of the underlying tables are in seperate databases on the same server. Inserts and deletes on the view work fine. We then...
1
by: Mark Flippin | last post by:
I'm evidently not understanding nested triggers and I'm looking for some help. I've an Invoice table (see below) that I want to enforce two actions via after triggers. The first trigger...
5
by: Praveen_db2 | last post by:
Hi All Are there DDL triggers in DB2? By DDL triggers I mean the triggers which can be fired on ALTER, DELETE, DROP, GRANT and REVOKE commands. Such triggers are there in Oracle and SQL Server. I...
4
by: JoeyD | last post by:
With SQL 2005, did Microsoft introduce login triggers? I seem to remember hearing something about it but, I don't remember exactly what I heard. Thanks, JD
1
by: Miaaa Mukherjee | last post by:
Hello, I want to know something about the stored procedures and triggers which are used. I want to know that the procedures n triggers r created in SQL Server 2005 or...
2
by: kwilla | last post by:
I need a way to view all existing triggers in our MS Server database especially since I don't know what tables are involved. Thanks, kwilla
11
by: Anthony Paul | last post by:
Hello everyone, I am involved in a scenario where there is a huge (SQL Server 2005) production database containing tables that are updated multiple times per second. End-user reports need to be...
1
by: WayneW | last post by:
I wrote some triggers in SQL 2000, and could update etc them through the User interface in the design screen. Where has this been moved in SQL 2005.
3
by: satchi | last post by:
Ok this should be a simple question but it's seemingly difficult (or something's wrong w/ my SQL Server Managmenet Sudio). I have created new triggers in SQL Server 2000 by clicking on Managing...
5
by: FP | last post by:
Hi, i have read that with Visual Studio 2005 it's possible to create stored procedures and triggers for a Sql Server 2005 database. I must admit that i have already written the stored procedures...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.