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 9218599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:54:05+00:00 2026-06-18T02:54:05+00:00

EDIT: I added all of the code for the logon trigger. Why am I

  • 0

EDIT: I added all of the code for the logon trigger.

Why am I getting some incorrect results when trying to retrieve the total time it takes for a query to run? This is within the context of a logon trigger on SQL Server 2008 R2.

For testing purposes, I want to get the a rough estimate of the total time it takes for a logon trigger to run. Here is a small sample of the results:

LogonId  AppId  TotalLogonTime (in MS)
 101      1        0
 253      2        3
 289      2        3
 985      1       -3
 325      1        0

How can the total time evaluate to be negative? Out of the 2.3 million executions so far, there have been 25 that resulted in a time of -3 ms. Here is the code:

CREATE trigger [trgAudit]
on all server
with execute as 'TriggerLogonUser'
for logon
as
begin
    set nocount on
    set transaction isolation level read committed

declare
     @LoginTime datetime
    ,@IsWindowsUser bit
    ,@TotalLogonTimeMS int
    ,@ClientNetAddress varchar(48)
    ,@MacAddress nchar(12)
    ,@CurrentUserId int -- UserNames
    ,@ApplicationId int
    ,@HonId int --HostName
    ,@LogonId int --Logon
    ,@MacId int -- MacAddress
    ,@CnaId int -- ClientNetAddress

begin try
    /*
    *** Get the login time, user type, mac address, and client net address  
    */
 select
      @LoginTime = getdate(),
      @IsWindowsUser = case
                          when len(nt_domain) = 0
                             then 0
                          else 1
                       end,
      @MacAddress = p.net_address,
      @ClientNetAddress = convert(varchar(48),connectionproperty('client_net_address'))
 from sys.sysprocesses p
 where 
      p.spid = @@spid

 /*
 *** Client Net Address
 */          
      select top 1
           @CnaId = CnaId
      from master.sysmaintenance.ClientNetAddress with(index(IX_CnaAddress))
      where @ClientNetAddress = CnaAddress

      --if the ip does not exist, insert it.
      if @CnaId is null
           begin
                insert master.sysmaintenance.ClientNetAddress(CnaAddress)
                     values (@ClientNetAddress)
                select @CnaId = @@identity
           end

 /*
 *** Applications
 */     
      select top 1
           @ApplicationId = AppId
      from master.sysmaintenance.Applications with(index(IX_AppName))
      where app_name() = AppName

      if @ApplicationId is null
           begin
                insert master.sysmaintenance.Applications (AppName)
                     values (app_name())
                select @ApplicationId = @@identity
           end

 /*
 *** HostName
 */     
      select top 1
           @HonId = HonId
      from master.sysmaintenance.HostName with(index(IX_HonName))
      where HonName = host_name()

      if @HonId is null
           begin
                insert master.sysmaintenance.HostName
                     values (host_name())

                select @HonId = @@identity
           end

 /*
 *** UserNames
 */     
      select top 1
           @CurrentUserId = UsnId
      from master.sysmaintenance.Usernames with(index(IX_UsnName))
      where UsnName = original_login()

      if @CurrentUserId is null
           begin
                insert master.sysmaintenance.Usernames
                     values (original_login())

                select @CurrentUserId = @@identity
           end

 /*
 *** MacAddress
 */     
      select top 1
           @MacId = MacId
      from master.sysmaintenance.MacAddress with(index(IX_MacAddress))
      where MacAddress = @MacAddress

      -- same logic is continued as in the applications
      if @MacId is null
           begin
                insert master.sysmaintenance.MacAddress (MacAddress)
                     values (@MacAddress)

                select @MacId = @@identity
           end

 /*
 *** Get the total logon time
 */     
 select @TotalLogonTimeMS = datediff(ms,@LoginTime, getdate())

  -- insert ids of the data gathered on the logon event into the logon table.


           insert master.sysmaintenance.Logon ( LogAppId,
                                                LogHonId,
                                                IsWindowsLogon,
                                                CurrentLogonId,
                                                LogonDatetime,
                                                LogCnaId,
                                                LogMacId,
                                                LogonTimeMS )
                values ( @ApplicationId,
                         @HonId,
                         @IsWindowsUser,
                         @CurrentUserId,
                         @LoginTime,
                         @CnaId,
                         @MacId,
                         @TotalLogonTimeMS
                       )
end try

begin catch
    print cast(error_number()    as nvarchar(11))
    print cast(error_severity()  as nvarchar(11))
    print cast(error_state()     as nvarchar(11))
    print cast(error_procedure() as nvarchar(126))
    print cast(error_line()      as nvarchar(11))
    print cast(error_message()   as nvarchar(2048))
end catch
end

Here is the DDL to the Logon Table:

CREATE TABLE [sysmaintenance].[Logon](
    [LogonId] [bigint] IDENTITY(1,1) NOT NULL,
    [LogAppId] [int] NULL,
    [LogHonId] [int] NULL,
    [LogMacId] [int] NULL,
    [LogCnaId] [int] NULL,
    [IsWindowsLogon] [bit] NULL,
    [CurrentLogonId] [int] NULL,
    [LogonDatetime] [datetime] NULL,
    [LogonTimeMS] [int] NULL
) ON [PRIMARY]

CREATE UNIQUE CLUSTERED INDEX [PK_Logon] ON [sysmaintenance].[Logon] 
(
    [LogonId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

Any help or insight is appreciated.

  • 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-06-18T02:54:06+00:00Added an answer on June 18, 2026 at 2:54 am

    GETDATE():

    Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.

    Now, bearing in mind that computer clocks drift, I’d imagine that if the computer’s time was corrected (either manually or automatically), the time reported by two successive calls to GETDATE() (that aren’t part of a single query) could go backwards. All of the datetime methods ultimately rely on the computer’s clock.

    It’s a shame you’re not logging one of these GETDATE() results alongside the timing, so you could see if they all occurred at the same “time”.

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

Sidebar

Related Questions

Edit: Added code (Exception on line 095, 5th time it's hit.) public DataTable ParseBarcodes(String[]
EDIT: i have added in all of my code (excluding package and imports.....) and
Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some
I have a piece of code: EDIT: The _penParams are initialized as the added
Greetings all. I am writing some code using the Boost Units library and have
In the following code why does mockTest.ToString() return Null? EDIT: Added comment into example
SOLVED: added some java to the Page 1 code to read the URL parameters
All the code is inline here: http://shanxinc.com/test/fancybox2/ All I'm trying to do is get
EDIT - added .h file I'm having difficulty trying to find the cause of
EDIT: I added in where I create new instance of the class Process as

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.