Friday, 17 February 2017

How to find restore database percentage in SQL server

select percent_complete, DATEADD(ss,estimated_completion_time/1000,getdate()) AS estimated_completion_time
from sys.dm_exec_requests where command like '%Restore%'

How to check expensive queries in SQL server

select top 50 qs.creation_time,
              qs.execution_count,
              qs.total_worker_time as total_cpu_time,
              qs.max_worker_time   as max_cpu_time,
              qs.total_elapsed_time,
              qs.max_elapsed_time,
              qs.total_logical_reads,
              qs.max_logical_reads,
              qs.total_physical_reads,
              qs.max_physical_reads,
              t.[text],
              qp.query_plan,
              t.dbid,
              t.objectid,
              t.encrypted,
              qs.plan_handle,
              qs.plan_generation_num
from   sys.dm_exec_query_stats qs
       cross apply sys.Dm_exec_sql_text(plan_handle) as t
       cross apply sys.Dm_exec_query_plan(plan_handle) as qp
--order  by qs.total_worker_time desc
--ORDER BY qs.total_logical_reads DESC -- logical reads
 ORDER BY qs.total_logical_writes DESC -- logical writes
-- ORDER BY qs.total_worker_time DESC -- CPU time

Last modified date of database in SQL Server

DECLARE @sqlString NVARCHAR(max)
DECLARE @union NVARCHAR(max)
SET @sqlString = ''
SET @union = ''
DECLARE @name nvarchar(50);

DECLARE crs CURSOR FOR
SELECT Name FROM sys.databases WHERE  state = 0
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
   SET @sqlString = @sqlString + @union
   SET @sqlString = @sqlString + '
    SELECT
    TOP 1
  ''' + @name + ''' as DBName, modify_date
  FROM
   [' + @name + '].sys.tables'

 SET @union = ' UNION '

    FETCH NEXT FROM crs INTO @name
END

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs;
DEALLOCATE crs;
EXEC(@sqlString)

How to set Read routing list in Always on - SQL server 2012

ALTER AVAILABILITY GROUP [AG-NAME]
MODIFY REPLICA ON
N'SERVER1' WITH
(PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=('SERVER3','SERVER2','SERVER4','SERVER5'))); 

Thursday, 17 September 2015

How to kill all sessions for single Database in SQL server



Please find below query:

DECLARE @DatabaseName nvarchar(50)
SET @DatabaseName = N'Name _of_Database'
--SET @DatabaseName = DB_NAME()

DECLARE @SQL varchar(max)
SET @SQL = ''

SELECT @SQL = @SQL + 'Kill ' + Convert(varchar, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId

-- SELECT @SQL
EXEC(@SQL)

What is Clutter in Outlook


Whenever we tired of unknown and unwanted mails, In such case we apply rules in outlook and unwanted mails automatically moved to the defined folder. But as we know there is limitation of defining number of rules in outlook. Sometime your rules gets disabled and to overcome this Microsoft introduced Clutter in Outlook. Clutter" can help you filter low-priority email, saving time for your most important messages. The email server keeps track of the email you read and the ones you don't. Once you turn it on, Clutter is automatic. As new email comes in, it takes messages you're most likely to ignore and puts them into the "Clutter" folder. The more you use it, the better it gets. And if you find Clutter isn't for you, you can turn it off.

My experience with Clutter is quit good and i appreciate for this amazing feature. 

Check if column exists or not in the SQL server

--method 1 IF EXISTS(SELECT 1 FROM sys.columns with (nolock)           WHERE Name = N'LoginName'           AND Object_ID = Objec...