DateTime Column Set to Past Date - sql

We have a SQL Server database for storing our transaction data. In each of the table, we have a LastUpdatedOn column which gets populated by using the GETDATE() function.
What we are seeing is when we have bulk update operations on these tables, at the end of bulk operation, the timestamp in the LastUpdatedOn column will be past date.
Eg: if the update operation started at time 10:00:00.000 AM, the GETDATE() function will select that as the time and the LastUpdatedOn column will be populated with datetime as 10:00:00.000 AM. If the script takes 30 seconds to execute, the at 10:00:30.000 AM, when the scripts completes the executed, the lastupdateon column will be populated with a past date.
See the sample script below:
SELECT TOP 100000 *
INTO #tmp
FROM dbo.Test
BEGIN TRAN
UPDATE L
SET l.lastupdateon = getdate()
FROM dbo.Test L
JOIN #tmp T ON T.entitykey = L.entitykey
COMMIT TRAN
The issue which we are running into is, we have a delta operation that has from and to dates. This operation will return any records that got updated between the from and to date. In this case, if delta operation gets called at 10:00:15.000 AM, the records that got updated as a part of the script will not be pulled even though the record got updated at 10:00:00.000 AM. These records will never get pulled.
Is there any solution for this problem other than making the transaction serializable?
Regards,
John

The fundamental problem is that the update takes time, so there will always be a delay between when you tell SQL Server to write a value to the table and when that value is committed and visible to others.
You need to address this in your delta operation. Is only one process writing to the table? If so, newly committed rows will always have a LastUpdatedOn value that is after everything else in the table. In this case, the delta operation should keep track of the max LastUpdatedOn value it got last time and look for anything greater than that.

Related

Is it possible to have same SYSDATETIME result for 2 transactions

According to documentation, the precision of SYSDATETIME() function in sql server is approximately 100 nanoseconds. I have seen that just like GETDATE(), the SYSDATETIME function also returns the same result within a transaction. Also, the time differs in two batches separated by GO.
Now my real question is, is it safe to assume that two transactions will always have different SYSDATETIME, no matter how close to concurrency they can reach within the same server/database instance, irrespective of the number of cores/hardware, the server has?
Background: I am trying to implement audit on an existing database using temporal tables. We are already keeping a modified by column in all tables. But we cannot identify who deleted a record using temporal tables. So I was thinking of dumping user id (end user's id) into a table for all transactions. So, if the time matches with temporal table, I might be able to identify the user based on date-time.
First i need to inform you that GETDATE() and SYSDATETIME gives the different datetime formate. SYSDATETIME() will give you result as follow - 2019-06-03 16:11:07.3683245 and GETDATE() will - 2019-06-03 16:11:07.367. Now the things is you need to add two columns in a table for which user updated the records and what time. And if you are not using any time consuming process between update temporal table and update main table than it will get same time both. But if any reason it will take time to update both record than it can be different time in both table.
You can use Declare method in sql to have same datetime in both table. No problem when it will update at different time. you can use Declare method to get datetime same as like follow.
Declare #date Datetime2 = SYSDATETIME()
Select #Date
You can use #Date When you are updating datetime in query.
I hope it will work.

Show Deleted Records point in time SQL Server

I want to write a function / stored procedure where I want to pass a date and time and get all the rows that exist for a particular date and time, if records were deleted after that date and time it should show them otherwise no.
Let's say I have a table students (id, name, createdate) with 100 rows today morning before 10:30 am, I delete 30 records today at 10:30 am and I run my function with today's date and time 10:35, it should return 70 records, but if I run the function with an older date like today's date at 09:30 am, it should return 100 records.
Please reply with the best possible solutions for this in SQL Server. I don't want to create a separate table of deleted items and then compare and show with that.
DELETE removes the rows, they are not available to view later on.
Unless you are using Sql Server 2016+ and then you could use a System versioned temporal table.
You could create an IsActive column or something to that effect, and instead of DELETE you UPDATE tbl SET IsActive = 0, dateInactivated = getdate() WHERE...
Then in your stored procedure you would:
SELECT *
FROM tbl
WHERE IsActive = 1
OR (IsActive = 0 AND dateInactivated > #myDate)
(EDIT reason: helpful comments below)

Query changed records in SQL using a created / modified time stamp column?

I'm trying to query my SQL Server 2008 R2 database to find out records that were put into a cancelled state during a given time period. Each record consist of one row in the database with a Created By timestamp, and a modified by timestamp. I can't change the schema of the database.
Each record has a "cancel state" column that is set to 0 or 1 (N and Y). I need to run a nightly job/query that will show all of the cancelled records from the day.
Simple enough, when the record is cancelled the modified timestamp is updated, so my query looks like
SELECT *
FROM mytable
WHERE CONVERT(DATE, a.modified_timestamp) = CONVERT(DATE, CURRENT_TIMESTAMP)
AND cancel_flag = '1'
The issue I am running into, if any other data is modified in the record, the timestamp is updated so the query will give me duplicate results if some other value in the row is changed. Is there an easy way to do this without changing the schema or adding my own table for tracking?

Is it possible to update a Sybase datetime column that defaults to current time?

A Sybase table with a column called "update_time" is a datetime type and defaults to the current time whenever the row is updated. However, I want to update the "update_time" of a row to a time in the past, but it just keeps putting to the current time. Is it possible to make this kind of change?
The query I'm using to try the update:
update table set update_time = '09-21-2015 12:00:00.000 PM' where id=1;
After I run this, the update_time for that row just goes to the current time when I run the query, not that actual time in the query.

Is there a way to set default value of field as other field in SQL Server 2005?

I have a table with 3 columns. One is sessionID as int, other one sessionLength t as int and other sessionActualLength as int.
sessionLength is value calculated for time being in online chat in seconds. sessionActualLength comes in place where in certain hours users get bonuses for being online and each second of their time actually means 1.12 seconds.
I want to calculate sessionActualLength without actually updating this column so each time sessionLength is updates and there is a certain time then sessionActualLength getting higher than actual time.
Sounds like a job for SQL Server Triggers:
CREATE TRIGGER MyTrigger on MyTable
FOR UPDATE, INSERT AS
UPDATE dbo.MyTable
SET column1 = (SELECT column2 FROM inserted)
This is from memory, but I'm pretty sure it's good.