How to get delete/Update row + SQL
-
Hi, How can i get the full record that is delete/Update via
exec sp_trace_setevent @TraceId
I mean: I have table with this columnsId int ,Name nvarchar(max) ,Family nvarchar(max)
with the values:1,jojoba,alin 2,babk,babyi
... now i when i update like this:UPDATE tblName
SET Name='okki', Family='koki' where Id=2now I wanna this in my trace:
2,babk,babyi Deleted 2,ooki,koki Inserted
-
Hi, How can i get the full record that is delete/Update via
exec sp_trace_setevent @TraceId
I mean: I have table with this columnsId int ,Name nvarchar(max) ,Family nvarchar(max)
with the values:1,jojoba,alin 2,babk,babyi
... now i when i update like this:UPDATE tblName
SET Name='okki', Family='koki' where Id=2now I wanna this in my trace:
2,babk,babyi Deleted 2,ooki,koki Inserted
Still working on that audit? :) Stop wasting time, if your customer really needs the old values of the record, then you'd be best of with doing only inserts; somebody wants to update a record? Mark the current one as outdated (datetime) and insert a new one. Yes, that'll make your database huge, but logging every action would result in yet even more data, since you're not only storing the new values (ignoring the old) - but also the command. You best rethink your requirements here.
Bastard Programmer from Hell :suss:
-
Still working on that audit? :) Stop wasting time, if your customer really needs the old values of the record, then you'd be best of with doing only inserts; somebody wants to update a record? Mark the current one as outdated (datetime) and insert a new one. Yes, that'll make your database huge, but logging every action would result in yet even more data, since you're not only storing the new values (ignoring the old) - but also the command. You best rethink your requirements here.
Bastard Programmer from Hell :suss:
thanks for your attention! but its the end of story.
is it possible to get that info via sp_trace_setevent .
i know that it is in ldf file. plz give the help to close the question. -
thanks for your attention! but its the end of story.
is it possible to get that info via sp_trace_setevent .
i know that it is in ldf file. plz give the help to close the question.jojoba2011 wrote:
is it possible to get that info via
Not as far as I can see.
Text value dependent on the event class captured in the trace. However, if you are tracing a parameterized query, the variables will not be displayed with data values in the TextData column.
jojoba2011 wrote:
i know that it is in ldf file
How did you know?
Bastard Programmer from Hell :suss:
-
jojoba2011 wrote:
is it possible to get that info via
Not as far as I can see.
Text value dependent on the event class captured in the trace. However, if you are tracing a parameterized query, the variables will not be displayed with data values in the TextData column.
jojoba2011 wrote:
i know that it is in ldf file
How did you know?
Bastard Programmer from Hell :suss:
Thanks for reply!
i have got the row update old value when i go to table and manually change the value
"via sp_trace_setevent (Transact-SQL)" but cannot get the old value when using SP for update. Plz help to get the old value when runing SP -
Thanks for reply!
i have got the row update old value when i go to table and manually change the value
"via sp_trace_setevent (Transact-SQL)" but cannot get the old value when using SP for update. Plz help to get the old value when runing SPA trace shows the commands written to the server; not the values that the server holds. A trace will not show the current values. Executing a SP with some values might even require different permissions than reading those values. As said, this is the wrong approach. If your customers need the old and the new values, then you should not update or delete, just insert.
Bastard Programmer from Hell :suss:
-
Hi, How can i get the full record that is delete/Update via
exec sp_trace_setevent @TraceId
I mean: I have table with this columnsId int ,Name nvarchar(max) ,Family nvarchar(max)
with the values:1,jojoba,alin 2,babk,babyi
... now i when i update like this:UPDATE tblName
SET Name='okki', Family='koki' where Id=2now I wanna this in my trace:
2,babk,babyi Deleted 2,ooki,koki Inserted
As Eddy has said, you need to address your requirements first! Attempting to retain every change, both from and to via the .ldf is not going to work. Eddies suggestion that you make copies of records instead of updating is valid (I have used this under duress), marking deleted records as Disabled is also a recognised solution. Go back to your requirements and reassess them in the light of what you have learned over the last few weeks trying to use the wrong design to achieve a desired result.
Never underestimate the power of human stupidity RAH
-
A trace shows the commands written to the server; not the values that the server holds. A trace will not show the current values. Executing a SP with some values might even require different permissions than reading those values. As said, this is the wrong approach. If your customers need the old and the new values, then you should not update or delete, just insert.
Bastard Programmer from Hell :suss:
I get Old Value and New Value u can see the red color
part:OldName,OldFamily--->UpdatedName,UpdatedFamily
via this trace :/****************************************************/
/* Created by: SQL Profiler */
/* Date: 16/04/2009 12:29:20 */
/****************************************************/-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 500
declare @OutputFileName nvarchar(200)
declare @EndTime datetimeSET @OutputFileName = 'C:\STrace' +
CONVERT(VARCHAR(20), GETDATE(),112) +
REPLACE(CONVERT(VARCHAR(20), GETDATE(),108),':','')SET @EndTime = DATEADD(mi,30,getdate())
-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network shareexec @rc = sp_trace_create @TraceID output, 0, @OutputFileName, @maxfilesize, @EndTime
if (@rc != 0) goto error-- Client side File and Table cannot be scripted
-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 10, 1, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 10, 16, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 9, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 16, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 18, @onexec sp_trace_setevent @TraceID, 41, 1, @on
exec sp_trace_setevent @TraceID, 41, 6, @on
exec sp_trace_setevent @TraceID, 41, 9, @on
exec sp_trace_setevent @TraceID, 41, 10, @on
exec sp_trace_setevent @TraceID, 41, 11, @on
exec sp_trace_setevent @TraceID, 41, 12, @on
exec sp_trace_setevent -
Hi, How can i get the full record that is delete/Update via
exec sp_trace_setevent @TraceId
I mean: I have table with this columnsId int ,Name nvarchar(max) ,Family nvarchar(max)
with the values:1,jojoba,alin 2,babk,babyi
... now i when i update like this:UPDATE tblName
SET Name='okki', Family='koki' where Id=2now I wanna this in my trace:
2,babk,babyi Deleted 2,ooki,koki Inserted
As stated you are going at it from the wrong direction. Other solutions include: - using the OUTPUT Clause[^] - using trigger[^] both can track the data. If the code cannot be changed the trigger would be the way to go.
-
As stated you are going at it from the wrong direction. Other solutions include: - using the OUTPUT Clause[^] - using trigger[^] both can track the data. If the code cannot be changed the trigger would be the way to go.
What does the trigger-solution provide, besides extra trouble in maintainability? We're not synchronizing on every record, and he merely needs a duplicate. Changing the command being executed would be a tad more efficient than adding triggers here to copy each and every reveived value to another table (with the same structure).
Bastard Programmer from Hell :suss:
-
What does the trigger-solution provide, besides extra trouble in maintainability? We're not synchronizing on every record, and he merely needs a duplicate. Changing the command being executed would be a tad more efficient than adding triggers here to copy each and every reveived value to another table (with the same structure).
Bastard Programmer from Hell :suss:
Since you can put logic in a trigger to only pull what you want this could create the table of updated columns. I did say that trigger might be better if the main code could not be changed.
-
Since you can put logic in a trigger to only pull what you want this could create the table of updated columns. I did say that trigger might be better if the main code could not be changed.
-
I get Old Value and New Value u can see the red color
part:OldName,OldFamily--->UpdatedName,UpdatedFamily
via this trace :/****************************************************/
/* Created by: SQL Profiler */
/* Date: 16/04/2009 12:29:20 */
/****************************************************/-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 500
declare @OutputFileName nvarchar(200)
declare @EndTime datetimeSET @OutputFileName = 'C:\STrace' +
CONVERT(VARCHAR(20), GETDATE(),112) +
REPLACE(CONVERT(VARCHAR(20), GETDATE(),108),':','')SET @EndTime = DATEADD(mi,30,getdate())
-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network shareexec @rc = sp_trace_create @TraceID output, 0, @OutputFileName, @maxfilesize, @EndTime
if (@rc != 0) goto error-- Client side File and Table cannot be scripted
-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 10, 1, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 10, 16, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 9, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 16, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 18, @onexec sp_trace_setevent @TraceID, 41, 1, @on
exec sp_trace_setevent @TraceID, 41, 6, @on
exec sp_trace_setevent @TraceID, 41, 9, @on
exec sp_trace_setevent @TraceID, 41, 10, @on
exec sp_trace_setevent @TraceID, 41, 11, @on
exec sp_trace_setevent @TraceID, 41, 12, @on
exec sp_trace_seteventIf you run it manually, you'll see the text as sent to the server. A trace is meant to "trace" what commands are being executed. AFAIK, it wouldn't even remotely interested in the "current" values. Repeating the question will not change the answer.
Bastard Programmer from Hell :suss:
-
Hi, How can i get the full record that is delete/Update via
exec sp_trace_setevent @TraceId
I mean: I have table with this columnsId int ,Name nvarchar(max) ,Family nvarchar(max)
with the values:1,jojoba,alin 2,babk,babyi
... now i when i update like this:UPDATE tblName
SET Name='okki', Family='koki' where Id=2now I wanna this in my trace:
2,babk,babyi Deleted 2,ooki,koki Inserted
May I offer a possible solution, if all of the database actions are being "called" by a front end application then another method to maintain an audit trail is to simply append and database change details to a ascii log file (simple text file). :) For example where I work if anybody makes a change to any database field the action is recorded also to the log file. Then if we need to access who made the changes and what changes were made on any particular date in time, even years ago it's a simple matter to check the log file. When it get's too big archive it and start a new one. :cool:
-
May I offer a possible solution, if all of the database actions are being "called" by a front end application then another method to maintain an audit trail is to simply append and database change details to a ascii log file (simple text file). :) For example where I work if anybody makes a change to any database field the action is recorded also to the log file. Then if we need to access who made the changes and what changes were made on any particular date in time, even years ago it's a simple matter to check the log file. When it get's too big archive it and start a new one. :cool:
Thanks all for Reply. Can u give me a small example on how to do that? I exactly wanna this.
-
If you run it manually, you'll see the text as sent to the server. A trace is meant to "trace" what commands are being executed. AFAIK, it wouldn't even remotely interested in the "current" values. Repeating the question will not change the answer.
Bastard Programmer from Hell :suss:
thanks for you reply for both C# and Database pages. But isnt it possible to have when running update SP. Its very important to do that . if possible then its end of story. Q2)is it possible to have the OLD trace if the user delete the .trc file from SQL.
-
Thanks all for Reply. Can u give me a small example on how to do that? I exactly wanna this.
Sure, this is a snippet from a routine in VB6, but it would be easy to convert it to VB.Net. In this instance I'm recording the method of payment and amounts from each transaction. If the log.txt file does not exist then create it, otherwise append to it. You will notice this particular log file is called LogReturns.txt and it's in the root directory of the program executable. Hope this is what you are looking for.
On Error Resume Next
Dim fso As New Scripting.FileSystemObject
Dim mFile As String
Dim txtfile As Object
mFile = "/LogReturns.txt"
If fso.FileExists(App.Path & mFile) Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.OpenTextFile(App.Path & "/LogReturns.txt", ForAppending)
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile(App.Path & "/LogReturns.txt", True)
End If
Dim mVar As Integer
txtfile.WriteLine (" ")
txtfile.WriteLine ("Receipt Number : " & recReceipt.Fields("ReceiptNum"))
txtfile.WriteLine ("Member Number : " & recReceipt.Fields("MembNum"))
For mVar = 0 To intMoviesReturnCount - 1
txtfile.WriteLine ("Movie Number(s): " & arrMoviesReturned(mVar))
Next mVar
txtfile.WriteLine ("Rec Date & Time: " & recReceipt.Fields("ReceiptDateTime"))
txtfile.WriteLine ("Amount Payable : " & Format(recReceipt.Fields("Amount"), "$0.00"))
' v1.0.159 : 26-Jul-2006 : JPG : Added CashNett value to report
txtfile.WriteLine ("Cash Nett : " & Format(recReceipt.Fields("CashNett"), "$0.00"))
txtfile.WriteLine ("Cash Tendered : " & Format(recReceipt.Fields("CashTendered"), "$0.00"))
txtfile.WriteLine ("Eftpos Selected: " & Format(recReceipt.Fields("Eftpos"), "$0.00"))
txtfile.WriteLine ("Cheque Provided: " & Format(recReceipt.Fields("Cheque"), "$0.00"))
txtfile.WriteLine ("Credit Card : " & Format(recReceipt.Fields("CreditCard"), "$0.00"))
txtfile.WriteLine ("GiftCard Used : " & Format(recReceipt.Fields("GiftCardUsed"), "$0.00"))
txtfile.WriteLine ("Discount : " & Format(recReceipt.Fields("Discount"), "$0.00"))
txtfile.WriteLine ("Transfer : " & Format(recReceipt.Fields("DebitMemberBalance"), "$0.00"))txtfile.WriteLine (" ") txtfile.WriteLine ("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*") txtfile.Close Set fso = Nothing
-
Sure, this is a snippet from a routine in VB6, but it would be easy to convert it to VB.Net. In this instance I'm recording the method of payment and amounts from each transaction. If the log.txt file does not exist then create it, otherwise append to it. You will notice this particular log file is called LogReturns.txt and it's in the root directory of the program executable. Hope this is what you are looking for.
On Error Resume Next
Dim fso As New Scripting.FileSystemObject
Dim mFile As String
Dim txtfile As Object
mFile = "/LogReturns.txt"
If fso.FileExists(App.Path & mFile) Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.OpenTextFile(App.Path & "/LogReturns.txt", ForAppending)
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile(App.Path & "/LogReturns.txt", True)
End If
Dim mVar As Integer
txtfile.WriteLine (" ")
txtfile.WriteLine ("Receipt Number : " & recReceipt.Fields("ReceiptNum"))
txtfile.WriteLine ("Member Number : " & recReceipt.Fields("MembNum"))
For mVar = 0 To intMoviesReturnCount - 1
txtfile.WriteLine ("Movie Number(s): " & arrMoviesReturned(mVar))
Next mVar
txtfile.WriteLine ("Rec Date & Time: " & recReceipt.Fields("ReceiptDateTime"))
txtfile.WriteLine ("Amount Payable : " & Format(recReceipt.Fields("Amount"), "$0.00"))
' v1.0.159 : 26-Jul-2006 : JPG : Added CashNett value to report
txtfile.WriteLine ("Cash Nett : " & Format(recReceipt.Fields("CashNett"), "$0.00"))
txtfile.WriteLine ("Cash Tendered : " & Format(recReceipt.Fields("CashTendered"), "$0.00"))
txtfile.WriteLine ("Eftpos Selected: " & Format(recReceipt.Fields("Eftpos"), "$0.00"))
txtfile.WriteLine ("Cheque Provided: " & Format(recReceipt.Fields("Cheque"), "$0.00"))
txtfile.WriteLine ("Credit Card : " & Format(recReceipt.Fields("CreditCard"), "$0.00"))
txtfile.WriteLine ("GiftCard Used : " & Format(recReceipt.Fields("GiftCardUsed"), "$0.00"))
txtfile.WriteLine ("Discount : " & Format(recReceipt.Fields("Discount"), "$0.00"))
txtfile.WriteLine ("Transfer : " & Format(recReceipt.Fields("DebitMemberBalance"), "$0.00"))txtfile.WriteLine (" ") txtfile.WriteLine ("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*") txtfile.Close Set fso = Nothing
thanks for ans. But i wanna to get the info from my SQL database . i wanna to have every thing the user changed,Updated,Inserted,Deleted. So i can trace that.
-
thanks for ans. But i wanna to get the info from my SQL database . i wanna to have every thing the user changed,Updated,Inserted,Deleted. So i can trace that.
Hi jojoba2011, You get the values you need to save from the front end application, what I mean to say is the values you are using in the database insert or update query are also used for the text report audit trail. In the case of the original values that are being replaced, they are stored as a variable when the update/insert page is first populated. Do you understand what I'm saying?
-
Hi jojoba2011, You get the values you need to save from the front end application, what I mean to say is the values you are using in the database insert or update query are also used for the text report audit trail. In the case of the original values that are being replaced, they are stored as a variable when the update/insert page is first populated. Do you understand what I'm saying?
first of all thanks for your attention! Sorry i cant understand! I think that u add values to txt when inserting them to Database. Correct? But i wanna to get it from DB.even if the text file deleted i can get info back.