Terminating Inactive SQL Sessions

  • We have users that log into a SQL database and then forget to log out of it before heading home.

    Is there a way within SQL to terminate inactive sessions?

    Thank you.

    Roger

  • Try this. You may want to add more conditions like last batch time.

    declare @spidstr varchar(8000)

    select @spidstr=coalesce(@spidstr,'')+'kill '+convert(varchar, spid)+ '; '

    from master..sysprocesses WHERE dbid=db_id('master') and open_tran = 0 and status = 'sleeping'

    print @spidstr

    --EXEC(@spidstr)

  • I'd be very careful killing jobs that are "sleeping". Why would you want to delete the jobs? Are you trying to do a restore and have an active thread into the database?

    -- You can't be late until you show up.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply