Search

Monday, March 24, 2014

T-Log space used by which transaction

Sometime T-Log space is increasing very fast and we are not aware that which transaction is culprit. You can use below DMV to check which transaction is culprit.

SELECT ST.Session_ID AS [Session ID], DT.Transaction_ID AS [Transaction ID], DB_NAME(DT.Database_ID) AS [DataBase Name], 
CASE Database_Transaction_Type WHEN 1 THEN 'Read / Write' WHEN 2 THEN 'Read Only' WHEN 3 THEN 'System' END AS [Transaction Type], 
CASE Database_Transaction_State WHEN 1 THEN 'Not Initialized' WHEN 3 THEN 'Transaction No Log' WHEN 4 THEN 'Transaction With Log' WHEN 5 THEN 'Transaction Prepared' WHEN 10 THEN 'Commited' WHEN 11 THEN 'Rolled Back' WHEN 12 THEN 'Commited And Log Generated' END AS [Transaction State], 
Database_Transaction_Begin_Time AS [Transaction Begin Time], SP.HostName AS [Host Name], SQLT.Text, SP.LogiName AS [Login Name], SP.LastWaitType AS [Last Wait Type], SP.Status, 
Database_Transaction_Log_Bytes_Reserved AS [Log Bytes Reserved], Database_Transaction_Log_Record_Count AS [Log Record Count], Database_Transaction_Log_Bytes_Used AS [Log Bytes Used], 
(Database_Transaction_Log_Bytes_Used + Database_Transaction_Log_Bytes_Reserved )/1024 AS [Total Log Space Used KB]
FROM SYS.DM_TRAN_Database_TransactionS AS DT JOIN SYS.DM_TRAN_SESSION_TransactionS AS ST ON DT.Transaction_ID=ST.Transaction_ID JOIN SYS.SYSPROCESSES AS SP ON SP.SPID = ST.SESSION_ID CROSS APPLY SYS.DM_EXEC_SQL_TEXT(SP.SQL_HANDLE) SQLT 
WHERE DT.Transaction_ID > 1000 AND ST.Session_ID > 50


Tuesday, March 18, 2014

Change Batch Separator

By default GO is the batch separator in SQL Server. GO is not a Transact SQL statement. It is a command recognized by SQL server as batch separator. You can change the batch separator (GO) in SQL server.
Follow below Steps to change the batch separator in SSMS:
  • Start SSMS
  • Go to Tools –> Options
  • Click on the “Query Execution” node
  • Notice that we have an option to change the Batch Separator
  • Change the batch separator
  • Click “OK”


Monday, March 3, 2014

List all Disabled Trigger

Use below query to get a list of all disabled trigger in a database:

SELECT db_name() AS [Database Name],
T.[Name] AS [TableName],
TR.[Name] AS [TriggerName],
[Status] = CASE WHEN OBJECTPROPERTY(TR.[ID], 'ExecIsTriggerDisabled') = 1
THEN 'Disabled' ELSE 'Enabled' END
FROM sysobjects T JOIN sysobjects TR ON T.[ID] = TR.[parent_obj]
WHERE T.[xtype] = 'U' AND TR.[xtype] = 'TR' AND OBJECTPROPERTY(TR.[ID], 'ExecIsTriggerDisabled') = 1 

Here you can use 0 to list Enabled triggers and 1 for Disabled triggers.


Monday, February 24, 2014

Last time SQL instance was restarted

You can use below methods to check the last time a SQL Server instance was started. 

Option 1 : Search the SQL Server error log for process start time. 

--search the current sql server error log for the the startup process id
DECLARE @ProcessType VARCHAR(64) = 'Server'
DECLARE @TextSearch VARCHAR(20) = 'Server process ID is'
DECLARE @LogType TINYINT = 1 -- 1 = SQL 2 = Agent
DECLARE @LogNum TINYINT = 0 -- 0 = current 1 =.1 etc
DECLARE @ErrLog AS TABLE([LogDate] DATETIME, [ProcessInfo] VARCHAR(64), [TEXT] VARCHAR(MAX))
INSERT INTO @ErrLog EXEC sys.xp_ReadErrorLog @LogNum, @LogType, @ProcessType, @TextSearch

--grab the first occurrence and report back the timestamp as the last startup
DECLARE @LastRestart DATETIME
SELECT @LastRestart = MIN(LogDate) FROM @ErrLog
SELECT @LastRestart AS [Last Restart]
GO

Option 1 : You can use below DMV also to check last start date of SQL Server instance

SELECT SQLServer_Start_Time AS [Last Start Date] FROM sys.dm_os_sys_info

Monday, February 17, 2014

Connect PerfMon to a Remote Server

I faced several connectivity issue during connecting Perfmon from remote server.

Server Access denied
Solution: PerfMon is trying to connect the remote server using logged in user account which have no access on remote server. Now, I need to run perfmon using a different user account which has access to the remote server.

DNS not able to resolve the server name
Solution: I have fixed the issue by adding an entry in the hosts file (C:\Windows\System32\drivers\etc).

Unable to connect the server
Solution: I have checked & found that Perfmon is trying to use Port 445 for connectivity. Telnet showed that port number 445 is blocked. I have added port into firewall exception & it worked.