| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | mkfrnsStarting Member
 
 
                                        2 Posts | 
                                            
                                            |  Posted - 2013-12-10 : 09:20:43 
 |  
                                            | Could any1 help me with query which fetches the inactive users and their last logged in date in sql server? |  |  
                                    | LPCPTStarting Member
 
 
                                    1 Post | 
                                        
                                          |  Posted - 2013-12-18 : 01:59:42 
 |  
                                          | Try this. It checks the error logs that's available. This assumes that successful logins are logged.Declare @period int --in daysset @period = 7 --Get all available logsCreate table #Logs ( Archivenr int, Logdate datetime, Size int)Insert #LogsExec XP_ENUMERRORLOGS--select * from #Logs--drop table #Logs--Loop through logs and extract successfull loginsCreate table ##ErrLog (logdate datetime, processinfo varchar(255), LoginStr varchar(1500))Declare @SqlCmd nvarchar(4000), @LogNr intDeclare LogCurs Cursor for Select Archivenr from #Logs where datediff(day, logdate, getdate()) <= @periodOpen LogCursFetch next from LogCurs into @LogNrWhile @@Fetch_Status = 0Begin	Set @SqlCmd = 'Insert ##ErrLog Exec master.dbo.xp_readerrorlog ' + 	Convert(varchar(4), @LogNr) + ', 1 , ''login succeeded'''	Exec (@SqlCmd)	Fetch next from LogCurs into @LogNrEndClose LogCursDeallocate LogCurs	--We only need entries for last x days (@period days)--Fix LoginStr to only contain the login name that will be in single quotes in the messageUpdate ##ErrLog set LoginStr = Substring(LoginStr, charindex('''', LoginStr, 1)+1, 			charindex('''', LoginStr, charindex('''', LoginStr, 1)+1)- charindex('''', LoginStr, 1)-1)	--Get loginid, login name and last login attempt date for logins that did not log in --in the last 3 months		Select Name,  Pre.last_logdate, hasaccess, isntname, isntuser, sysadminFrom	--Get all logins with no login entry in last @period days	(select SP.Name, hasaccess, isntname, isntuser, sysadmin		from sys.server_principals SP		inner join syslogins SL on SP.Sid = SL.Sid		where SP.Sid <> 0x01		--sa account		and type_desc in ('SQL_Login', 'WINDOWS_LOGIN')		and SL.name not in		(select LoginStr from ##ErrLog where datediff(day, logdate, getdate()) <= @period)		--and status <> 10	) as Post	Left outer join 	--Get last logdate before last 90 days	(Select LoginStr, max(logdate) as last_logdate from ##ErrLog		--where datediff(day, logdate, getdate()) > @period		group by LoginStr) as Pre	On Post.name = Pre.LoginStr		drop table #Logsdrop table ##ErrLogIntegrity rules |  
                                          |  |  |  
                                |  |  |  |  |  |