Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6642065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:53:10+00:00 2026-05-25T23:53:10+00:00

I have the following stored procedure to retrieve a new datetime: CREATE PROCEDURE [dbo].[GetTransactionTime]

  • 0

I have the following stored procedure to retrieve a new datetime:

CREATE PROCEDURE [dbo].[GetTransactionTime] 
AS
BEGIN
      SELECT [TransactionTime] = GetUTCDate()
END

To call this SP i use a simple C# dataset on a local DB.

Since the datetime from SQL has a precision of 3.3ms i have to wait at least 3.3ms to get an new datetime. See more about the precision at msdn: http://msdn.microsoft.com/en-us/library/ms187819.aspx

In the example below i will wait 5ms:

class Program
{
    static void Main(string[] args)
    {
        var adapter = new SPTableAdapters.TransactionTimeTableAdapter();
        do
        {
            GetTransactionTime(
                    new Func<DateTime>(() => { return (DateTime)adapter.GetTransactionTimeNOW(); })
                );
        } while (true);

    }

    public static DateTime lastTransactionTime = DateTime.MinValue;

    private static bool IsTransactionTimeUnique(DateTime newTransactionTime)
    {
        if (newTransactionTime > lastTransactionTime)
        {
            lastTransactionTime = newTransactionTime;
            return true;
        }
        return false;
    }

    public static void GetTransactionTime(Func<DateTime> funcGetTransactionTime)
    {
        DateTime newTransactionTime = funcGetTransactionTime();
        //MSSQL datetime has a off 3.33ms; try 5ms
        DateTime now = DateTime.UtcNow;
        DateTime max = now.AddMilliseconds(5);

        while (!IsTransactionTimeUnique(newTransactionTime))
        {
            DateTime backup = DateTime.UtcNow;
            if (backup > max) //if we tried more than 5ms.
            {

                //Always try once again.
                newTransactionTime = funcGetTransactionTime();

                if (IsTransactionTimeUnique(newTransactionTime))
                {
                    break;
                }

                Console.WriteLine("FAILED: Old datetime: " + lastTransactionTime.ToBinary() + " New datetime: " + newTransactionTime.ToBinary() + "Start: " + now.Millisecond + " End: " + backup.Millisecond + " Max: " + max.Millisecond);
                return; ;
            }

            newTransactionTime = funcGetTransactionTime();
        }
        Console.WriteLine("OK!");
    }
}

The output:

FAILED: Old: 487 New: 487 Start: 479 End: 484 Max: 484
FAILED: Old: 487 New: 487 Start: 484 End: 489 Max: 489
FAILED: Old: 487 New: 487 Start: 489 End: 494 Max: 494
OK!
FAILED: Old: 503 New: 503 Start: 495 End: 500 Max: 500
FAILED: Old: 503 New: 503 Start: 500 End: 505 Max: 505
OK!
FAILED: Old: 517 New: 517 Start: 510 End: 515 Max: 515
FAILED: Old: 517 New: 517 Start: 515 End: 520 Max: 520
FAILED: Old: 517 New: 517 Start: 520 End: 525 Max: 525
OK!
FAILED: Old: 533 New: 533 Start: 526 End: 531 Max: 531
FAILED: Old: 533 New: 533 Start: 531 End: 536 Max: 536
OK!
FAILED: Old: 550 New: 550 Start: 541 End: 546 Max: 546
FAILED: Old: 550 New: 550 Start: 546 End: 551 Max: 551
FAILED: Old: 550 New: 550 Start: 551 End: 556 Max: 556
OK!

MSSQL wont give me a unique datetime every 5ms, changing the 5ms to 20ms works but i don’t understand why the 5ms doesn’t work. Hope some one can clarify this.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:53:10+00:00Added an answer on May 25, 2026 at 11:53 pm

    Here’s couple of articles on the subject, but basically on Windows time gets updated less often than after every 3.3ms.

    http://www.grahamwideman.com/gw/tech/dataacq/wintiming.htm

    http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=85520

    There’s also a msdn article on the precision of GetTickCount:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following stored procedure: CREATE PROCEDURE testProc(IN p_idProject INTEGER) BEGIN DECLARE nowTime DATETIME;
I have the following stored procedure ALTER PROCEDURE [dbo].Test AS BEGIN CREATE TABLE ##table
I have a following stored procedure CREATE PROCEDURE [dbo].[MyStored] @state int AS SELECT blahblahblah
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I have the following stored procedure. ALTER PROCEDURE [dbo].[spList_Report] @id INT, @startDate DATETIME =
I am using Oracle 10g and I have the following stored procedure: CREATE OR
I have the following Stored Procedure ALTER PROCEDURE [dbo].[bt_BizForSale_GetByID] ( @ID int ) AS
I have the following Java stored procedure: CREATE OR REPLACE AND RESOLVE JAVA SOURCE
I have the following stored Procedure: SELECT tsks.grouping_ref, ttg.description AS grouping_desc, SUM(ts.booked_time) AS booked_time_total,
I have a problem with the following stored procedure CREATE PROCEDURE LockRoots -- Add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.