SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to identify a column update
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sebastian11c
Posting Yak Master

125 Posts

Posted - 04/09/2012 :  12:30:38  Show Profile  Reply with Quote
hi

i have a table called "info" with 20 columns, every deleted row when a user do an update from this table is saved in other table called "infobackup"

now i want to identify wich column was changed from every update because compare all te 20 columns to identify wich one has changed its to hard

any ideas, any query any help will be appreciate

many many thanks in advanced

JimL
SQL Slinging Yak Ranger

USA
1530 Posts

Posted - 04/09/2012 :  12:51:58  Show Profile  Visit JimL's Homepage  Reply with Quote
I take it your using a trigger.

You could add a new column in the backup table with a case statement

Something like

Colchanged = case when inserted.c1<> deleted.c1 then 'c1' when inserted.c2 <> deleted.c2 then 'c2' ..........

Jim
Users <> Logic
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/09/2012 :  12:52:38  Show Profile  Reply with Quote
Do you have :
a) a column (such as primary key) in your INFOBACKUP table that can be used to associate the rows to a the row in the INFO table?
and
b) a column (such as a timestamp or id number) in your INFORBACKUP table that can be used to identify the sequence of updates (i.e., to order the rows in the INFORBACKUP table in chronological order)?

If you do, then you can write a query to pick all the updated rows for any given row in the INFO table, order them chronologically and find which columns have changed.
Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 04/09/2012 :  13:13:53  Show Profile  Reply with Quote
thanks for your answer sunitabeck, yes i have a primarykey in my table "infobackup" and also i have a column with a getdate (),in my table "infobackup",, the problem is people can do updates from several columns at the same time,

any idea to identify the columns have changed??

thanks
Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 04/09/2012 :  13:16:16  Show Profile  Reply with Quote
thanks for tyour answer jimL
but people can do updates from several columns at the same time

any idea to solve this

tahnks in advanced
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 04/09/2012 :  14:56:15  Show Profile  Reply with Quote
so multiple column updates you will store all their details inside Colchanged ?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JimL
SQL Slinging Yak Ranger

USA
1530 Posts

Posted - 04/09/2012 :  15:18:02  Show Profile  Visit JimL's Homepage  Reply with Quote
1st are you inserting the record on the other table via a trigger on the original table?

Jim
Users <> Logic
Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 04/09/2012 :  16:48:01  Show Profile  Reply with Quote
yes im using a trigger , i insert all the columns from the table deleted in my table "infobackup" with an identity column and a date column,, but i dont save antything else,,

and i would like to create a query with the details of the columns that have changed
Go to Top of Page

JimL
SQL Slinging Yak Ranger

USA
1530 Posts

Posted - 04/09/2012 :  16:52:53  Show Profile  Visit JimL's Homepage  Reply with Quote
Ok I have an Idea.

Please post your Trigger.

Jim
Users <> Logic
Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 04/12/2012 :  17:46:27  Show Profile  Reply with Quote
sorry for the delay

this is my trigger, any correction please let me know "the columns are on spanish"

create TRIGGER [dbo].[pre_postUPDATE_info]
ON [dbo].[info]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON


INSERT INTO [dbo].[infobackup]
([IdVehiculo]
,[Placa]
,[Matricula]
,[Motor]
,[Serie]
,[Chasis]
,[EsImportado]
,[Manifiesto]
,[IdCiudadNacionalizacion]
,[FechaNacionalizacion]
,[Cilindraje]
,[Modelo]
,[CantidadPuertas]
,[CodigoFasecolda]
,[ValorFasecolda]
,[IdMarcaLinea]
,[IdClase]
,[IdColor]
,[IdServicio]
,[IdCarroceria]
,[IdTramiteDoc]
,[IdOrganismoDoc]
,[FechaExpedicionDoc]
,[Consecutivo]
,[FechaSOAT]
,[FechaRTM]
, valorcomercial
)

select IdVehiculo ,Placa, Matricula, Motor, Serie , Chasis ,
EsImportado ,
Manifiesto ,
IdCiudadNacionalizacion ,
FechaNacionalizacion ,
Cilindraje ,
Modelo ,
CantidadPuertas ,
CodigoFasecolda ,
ValorFasecolda ,
IdMarcaLinea ,
IdClase ,
IdColor ,
IdServicio ,
IdCarroceria ,
IdTramiteDoc ,
IdOrganismoDoc ,
FechaExpedicionDoc ,
Consecutivo ,
FechaSOAT ,
FechaRTM ,
ValorComercial
from deleted
end




thanks in advanced
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000