Tag Archives: SQL

Azure SQL Database Index Size

Recently I’ve been doing some work on optimising indexes within Azure SQL Database. The following SQL from Daniel is very useful in showing the status of all the current indexes within a SQL Database. The SQL works for both SQL Server 2014 onwards, plus Azure SQL Database. I have not tested it on any other versions.

This code returns the following:

  • Object Type
  • Object Name
  • Index Name
  • Index Type
  • Partition (if any)
  • Compression
  • Data Space
  • Fill Factor
  • Rows
  • Reserved MB
  • In Row Used MB
  • Row Overflow Used MB
  • Out of Row Used MB
  • Total Used MB
SELECT --- Schema, type and name of object and index:
	       REPLACE(obj.type_desc, '_', ' ') AS objectType
	,       sch.[name] + '.' + obj.[name] AS objectName
	,       ISNULL(ix.[name], '') AS indexName
	,       ix.type_desc AS indexType
	,       --- Partition number, if there are partitions:
	      (CASE COUNT(*) OVER (
				PARTITION BY ps.[object_id]
				,ps.index_id
				)             
			WHEN 1
				THEN ''             
			ELSE CAST(ps.partition_number AS VARCHAR(10))             
			END) AS [partition]
	,       --- Storage properties:
	       p.data_compression_desc AS [compression]
	,       ds.[name] + ISNULL('(' + pc.[name] + ')', '') AS dataSpace
	,       STR(ISNULL(NULLIF(ix.fill_factor, 0), 100), 4, 0) + '%' AS [fillFactor]
	,       --- The raw numbers:
	       ps.row_count AS [rows]
	,       STR(1.0 * ps.reserved_page_count * 8 / 1024, 12, 2) AS reserved_MB
	,       STR(1.0 * ps.in_row_used_page_count * 8 / 1024, 12, 2) AS inRowUsed_MB
	,       STR(1.0 * ps.row_overflow_used_page_count * 8 / 1024, 12, 2) AS RowOverflowUsed_MB
	,       STR(1.0 * ps.lob_used_page_count * 8 / 1024, 12, 2) AS outOfRowUsed_MB
	,       STR(1.0 * ps.used_page_count * 8 / 1024, 12, 2) AS totalUsed_MB
FROM sys.dm_db_partition_stats AS ps
INNER JOIN sys.partitions AS p ON     ps.[partition_id] = p.[partition_id]
INNER JOIN sys.objects AS obj ON     ps.[object_id] = obj.[object_id]
INNER JOIN sys.schemas AS sch ON     obj.[schema_id] = sch.[schema_id]
LEFT JOIN sys.indexes AS ix ON     ps.[object_id] = ix.[object_id]
	AND     ps.index_id = ix.index_id
--- Data space is either a file group or a partition function:
LEFT JOIN sys.data_spaces AS ds ON     ix.data_space_id = ds.data_space_id
--- This is the partitioning column:
LEFT JOIN sys.index_columns AS ixc ON     ix.[object_id] = ixc.[object_id]
	AND     ix.index_id = ixc.index_id
	AND     ixc.partition_ordinal > 0
LEFT JOIN sys.columns AS pc ON     pc.[object_id] = obj.[object_id]
	AND     pc.column_id = ixc.column_id
--- Not interested in system tables and internal tables:
WHERE obj.[type] NOT IN (
		'S'
		,'IT'
		)
ORDER BY sch.[name]
	,obj.[name]
	,ix.index_id
	,p.partition_number;

Example Results:

Delete Duplicate Records – Leave Only 1 Behind MS SQL

Recently I’ve had to delete duplicate records from the Record Link table in Microsoft Dynamics Nav.

Using the following SQL, (which uses the OVER clause for partitioning) this deletes all duplicate records, leaving the first unique item behind.

The SQL can be updated easily to function for other tables / scenarios. Currently it uses the following fields to identify duplicates:

  • Description
  • URL 1

The SQL does not look at the Notes, which is stored as a BLOB.

DELETE
FROM [Record Link]
WHERE [Link ID] IN (
		SELECT [Link ID]
		FROM (
			SELECT *
				,ROW_NUMBER() OVER (
					PARTITION BY [Record ID]
					,[Description]
					,[URL1] ORDER BY [Link ID]
					) AS [ItemNumber]
			FROM [Record Link]
			WHERE [Note] IS NULL
			) a
		WHERE ItemNumber > 1
		)

Suggesting MS SQL Index Creations Using sys.dm_db_missing_index_details

Within SQL Server, sys.dm_db_missing_index_details returns indexes which it believes are required by the Query Optimiser. Basically as queries are run in the background, the Query Optimiser makes a record of any optimisations it feels are necessary. Restarting the SQL server resets all these stats.

Using some SQL it is possible to identify potentially missing indexes, create sample SQL to build these indexes. I’ve found the following SQL useful:

SELECT mid.statement
	,migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure
	,'CREATE INDEX [missing_index_' + CONVERT(VARCHAR, mig.index_group_handle) + '_' + CONVERT(VARCHAR, mid.index_handle) + '_' + LEFT(PARSENAME(mid.statement, 1), 32) + ']' + ' ON ' + mid.statement + ' (' + ISNULL(mid.equality_columns, '') + CASE 
		WHEN mid.equality_columns IS NOT NULL
			AND mid.inequality_columns IS NOT NULL
			THEN ','
		ELSE ''
		END + ISNULL(mid.inequality_columns, '') + ')' + ISNULL(' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement
	,migs.*
	,mid.database_id
	,mid.[object_id]
FROM sys.dm_db_missing_index_groups mig
INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle
WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10
ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC

This produces results as follows:

Columns include:

  • statement – The table affected
  • improvement_measure – A generated measure of improvement (based on user cost, impact, seeks etc)
  • create_index_statement – A cut and paste friendly statement to add the index
  • group_handle – Identifies a group of missing indexes. This identifier is unique across the server.
  • unique_compiles – Number of compilations and recompilations that would benefit from this missing index group
  • user_seeks – Number of seeks caused by user queries that the recommended index in the group could have been used for
  • user_scans – Number of scans caused by user queries that the recommended index in the group could have been used for.
  • last_user_seek – Date and time of last seek caused by user queries that the recommended index in the group could have been used for.
  • last_user_scan – Date and time of last scan caused by user queries that the recommended index in the group could have been used for.
  • avg_total_user_cost – Average cost of the user queries that could be reduced by the index in the group.
  • avg_user_impact – Average percentage benefit that user queries could experience if this missing index group was implemented.
  • system_seeks – Number of seeks caused by system queries, such as auto stats queries, that the recommended index in the group could have been used for.
  • last_system_seek – Date and time of last system seek caused by system queries that the recommended index in the group could have been used for.
  • last_system_scan – Date and time of last system scan caused by system queries that the recommended index in the group could have been used for.
  • avg_total_system_cost – Average cost of the system queries that could be reduced by the index in the group.
  • avg_system_impact – Average percentage benefit that system queries could experience if this missing index group was implemented.
  • database_id – The ID number of the database
  • object_id – The ID number of the object

Microsoft Dynamics NAV – View All Active Sessions

With Microsoft Dynamics Nav, there are various ways of viewing all the active sessions within the system. The easiest of which is the “Sessions” page within the software itself:

sessions-menu-option

sessions-window

The downside of this, is that it only shows active sessions on the tier which the user is connected. This is ok for solutions where the system has a single tier, however it is common to have multiple tiers in order to spread load. Checking this way is time consuming as you need to check multiple tiers in order to get a session count.

However using SQL can be an easy way to get the number of active sessions:

SELECT [User ID]
	,[Server Instance Name]
	,[Server Computer Name]
	,[Database Name]
	,[Client Computer Name]
	,[Login Datetime]
FROM [dbo].[Active Session]

It is worth nothing that Nav tidies up this table automatically, but in some cases it may be incorrect. For example if a middle tier crashes out, it could be left with orphaned records until the tier starts up again (or another tier prunes the records down).

Convert MS SQL Query to MySQL Automatically

With a recent project, I’ve had to reproduce a table from a Microsoft SQL Database into a MySQL Database.

The table had lots of fields and would be very time consuming to write the SQL to create the table, but luckily I came across this tool to convert MSSQL to MySQL

MS SQL:

CREATE TABLE [dbo].[CRONUS UK Ltd_$Customer](
	[timestamp] [timestamp] NOT NULL,
	[No_] [varchar](20) NOT NULL,
	[Name] [varchar](50) NOT NULL,
	[Search Name] [varchar](50) NOT NULL,
	[Name 2] [varchar](50) NOT NULL,
	[Address] [varchar](50) NOT NULL,
	[Address 2] [varchar](50) NOT NULL,
	[City] [varchar](30) NOT NULL,
	[Contact] [varchar](50) NOT NULL,
	[Phone No_] [varchar](30) NOT NULL,
	[Telex No_] [varchar](20) NOT NULL,
	[Our Account No_] [varchar](20) NOT NULL,
	[Territory Code] [varchar](10) NOT NULL,
	[Global Dimension 1 Code] [varchar](20) NOT NULL,
	[Global Dimension 2 Code] [varchar](20) NOT NULL,
	[Chain Name] [varchar](10) NOT NULL,
	[Budgeted Amount] [decimal](38, 20) NOT NULL,
	[Credit Limit (LCY)] [decimal](38, 20) NOT NULL,
	[Customer Posting Group] [varchar](10) NOT NULL,
	[Currency Code] [varchar](10) NOT NULL,
	[Customer Price Group] [varchar](10) NOT NULL,
	[Language Code] [varchar](10) NOT NULL,
	[Statistics Group] [int] NOT NULL,
	[Payment Terms Code] [varchar](10) NOT NULL,
	[Fin_ Charge Terms Code] [varchar](10) NOT NULL,
	[Salesperson Code] [varchar](10) NOT NULL,
	[Shipment Method Code] [varchar](10) NOT NULL,
	[Shipping Agent Code] [varchar](10) NOT NULL,
	[Place of Export] [varchar](20) NOT NULL,
	[Invoice Disc_ Code] [varchar](20) NOT NULL,
	[Customer Disc_ Group] [varchar](10) NOT NULL,
	[Country_Region Code] [varchar](10) NOT NULL,
	[Collection Method] [varchar](20) NOT NULL,
	[Amount] [decimal](38, 20) NOT NULL,
	[Blocked] [int] NOT NULL,
	[Invoice Copies] [int] NOT NULL,
	[Last Statement No_] [int] NOT NULL,
	[Print Statements] [tinyint] NOT NULL,
	[Bill-to Customer No_] [varchar](20) NOT NULL,
	[Priority] [int] NOT NULL,
	[Payment Method Code] [varchar](10) NOT NULL,
	[Last Date Modified] [datetime] NOT NULL,
	[Application Method] [int] NOT NULL,
	[Prices Including VAT] [tinyint] NOT NULL,
	[Location Code] [varchar](10) NOT NULL,
	[Fax No_] [varchar](30) NOT NULL,
	[Telex Answer Back] [varchar](20) NOT NULL,
	[VAT Registration No_] [varchar](20) NOT NULL,
	[Combine Shipments] [tinyint] NOT NULL,
	[Gen_ Bus_ Posting Group] [varchar](10) NOT NULL,
	[Picture] [image] NULL,
	[Post Code] [varchar](20) NOT NULL,
	[County] [varchar](30) NOT NULL,
	[E-Mail] [varchar](80) NOT NULL,
	[Home Page] [varchar](80) NOT NULL,
	[Reminder Terms Code] [varchar](10) NOT NULL,
	[No_ Series] [varchar](10) NOT NULL,
	[Tax Area Code] [varchar](20) NOT NULL,
	[Tax Liable] [tinyint] NOT NULL,
	[VAT Bus_ Posting Group] [varchar](10) NOT NULL,
	[Reserve] [int] NOT NULL,
	[Block Payment Tolerance] [tinyint] NOT NULL,
	[IC Partner Code] [varchar](20) NOT NULL,
	[Prepayment %] [decimal](38, 20) NOT NULL,
	[Primary Contact No_] [varchar](20) NOT NULL,
	[Responsibility Center] [varchar](10) NOT NULL,
	[Shipping Advice] [int] NOT NULL,
	[Shipping Time] [varchar](32) NOT NULL,
	[Shipping Agent Service Code] [varchar](10) NOT NULL,
	[Service Zone Code] [varchar](10) NOT NULL,
	[Allow Line Disc_] [tinyint] NOT NULL,
	[Base Calendar Code] [varchar](10) NOT NULL,
	[Copy Sell-to Addr_ to Qte From] [int] NOT NULL,
	[Type of Supply Code] [varchar](10) NOT NULL,
	[Liq_ Payment Terms Code] [varchar](10) NOT NULL,
 CONSTRAINT [CRONUS UK Ltd_$Customer$0] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

MySQL:

CREATE TABLE `CRONUS UK Ltd_$Customer`(
	`timestamp` `timestamp` NOT NULL,
	`No_` varchar(20) NOT NULL,
	`Name` varchar(50) NOT NULL,
	`Search Name` varchar(50) NOT NULL,
	`Name 2` varchar(50) NOT NULL,
	`Address` varchar(50) NOT NULL,
	`Address 2` varchar(50) NOT NULL,
	`City` varchar(30) NOT NULL,
	`Contact` varchar(50) NOT NULL,
	`Phone No_` varchar(30) NOT NULL,
	`Telex No_` varchar(20) NOT NULL,
	`Our Account No_` varchar(20) NOT NULL,
	`Territory Code` varchar(10) NOT NULL,
	`Global Dimension 1 Code` varchar(20) NOT NULL,
	`Global Dimension 2 Code` varchar(20) NOT NULL,
	`Chain Name` varchar(10) NOT NULL,
	`Budgeted Amount` decimal(38, 20) NOT NULL,
	`Credit Limit (LCY)` decimal(38, 20) NOT NULL,
	`Customer Posting Group` varchar(10) NOT NULL,
	`Currency Code` varchar(10) NOT NULL,
	`Customer Price Group` varchar(10) NOT NULL,
	`Language Code` varchar(10) NOT NULL,
	`Statistics Group` int NOT NULL,
	`Payment Terms Code` varchar(10) NOT NULL,
	`Fin_ Charge Terms Code` varchar(10) NOT NULL,
	`Salesperson Code` varchar(10) NOT NULL,
	`Shipment Method Code` varchar(10) NOT NULL,
	`Shipping Agent Code` varchar(10) NOT NULL,
	`Place of Export` varchar(20) NOT NULL,
	`Invoice Disc_ Code` varchar(20) NOT NULL,
	`Customer Disc_ Group` varchar(10) NOT NULL,
	`Country_Region Code` varchar(10) NOT NULL,
	`Collection Method` varchar(20) NOT NULL,
	`Amount` decimal(38, 20) NOT NULL,
	`Blocked` int NOT NULL,
	`Invoice Copies` int NOT NULL,
	`Last Statement No_` int NOT NULL,
	`Print Statements` `tinyint` NOT NULL,
	`Bill-to Customer No_` varchar(20) NOT NULL,
	`Priority` int NOT NULL,
	`Payment Method Code` varchar(10) NOT NULL,
	`Last Date Modified` datetime NOT NULL,
	`Application Method` int NOT NULL,
	`Prices Including VAT` `tinyint` NOT NULL,
	`Location Code` varchar(10) NOT NULL,
	`Fax No_` varchar(30) NOT NULL,
	`Telex Answer Back` varchar(20) NOT NULL,
	`VAT Registration No_` varchar(20) NOT NULL,
	`Combine Shipments` `tinyint` NOT NULL,
	`Gen_ Bus_ Posting Group` varchar(10) NOT NULL,
	`Picture` blob NULL,
	`Post Code` varchar(20) NOT NULL,
	`County` varchar(30) NOT NULL,
	`E-Mail` varchar(80) NOT NULL,
	`Home Page` varchar(80) NOT NULL,
	`Reminder Terms Code` varchar(10) NOT NULL,
	`No_ Series` varchar(10) NOT NULL,
	`Tax Area Code` varchar(20) NOT NULL,
	`Tax Liable` `tinyint` NOT NULL,
	`VAT Bus_ Posting Group` varchar(10) NOT NULL,
	`Reserve` int NOT NULL,
	`Block Payment Tolerance` `tinyint` NOT NULL,
	`IC Partner Code` varchar(20) NOT NULL,
	`Prepayment %` decimal(38, 20) NOT NULL,
	`Primary Contact No_` varchar(20) NOT NULL,
	`Responsibility Center` varchar(10) NOT NULL,
	`Shipping Advice` int NOT NULL,
	`Shipping Time` varchar(32) NOT NULL,
	`Shipping Agent Service Code` varchar(10) NOT NULL,
	`Service Zone Code` varchar(10) NOT NULL,
	`Allow Line Disc_` `tinyint` NOT NULL,
	`Base Calendar Code` varchar(10) NOT NULL,
	`Copy Sell-to Addr_ to Qte From` int NOT NULL,
	`Type of Supply Code` varchar(10) NOT NULL,
	`Liq_ Payment Terms Code` varchar(10) NOT NULL,
 CONSTRAINT `CRONUS UK Ltd_$Customer$0` PRIMARY KEY 
(
	`No_` ASC
)
)

;

A simple find and replace in the new SQL to correct tinyint, and then the table can be easily created in the MySQL database.

SQL Remove Trailing Decimal Places

Within a recent project, I’ve been extracting data from Microsoft Dynamics NAV using Taskcentre.

Within NAV, decimals are stored as decimal(38,20), complete will all 20 decimal places..!

Dynamics NAV 20 Decimal Places

The following SQL query is very handy at removing the un-required decimal places (but keeping the accuracy of the data)

SELECT CONVERT(DOUBLE PRECISION, 1.99990000000000000000)
	,CONVERT(DOUBLE PRECISION, 2.00000000000000000000)
	,CONVERT(DOUBLE PRECISION, 3.00005077000000000000)

The results of the above query is:

1.9999
2
3.00005077

SQL Reduce Number of Decimal Places

SQL Server – Find Last Backup Date/Time

With SQL server and 3rd party backup software, it can sometimes be difficult to tell when the last SQL backup has been taken and if the backup software has actually performed the backup or not.

There are 3 main backup types in SQL Server:

  • Full
  • Incremental
  • Transaction Log

The following SQL query will show vital statistics, including when the last backup took place for each backup type. Handy for debugging and checking backup strategies..!

This post is not going to go into the details of how each one works, or recommendations for strategies etc. There are lots of details on guides on the internet, such as this one – Microsoft Technet – Understanding SQL Backups.

 SELECT  name ,
            recovery_model_desc ,
            state_desc ,
            d AS 'Last Full Backup' ,
            i AS 'Last Differential Backup' ,
            l AS 'Last log Backup'
    FROM    ( SELECT    db.name ,
                        db.state_desc ,
                        db.recovery_model_desc ,
                        type ,
                        backup_finish_date
              FROM      master.sys.databases db
                        LEFT OUTER JOIN msdb.dbo.backupset a ON a.database_name = db.name
            ) AS Sourcetable 
        PIVOT 
            ( MAX(backup_finish_date) FOR type IN ( D, I, L ) ) AS MostRecentBackup

For example:

SQL Last Backup

TaskCentre SQL Performance – One way to improve performance

When using the “ODBC” or “OLEDB” tools in TaskCentre, it may appear that a single query is executed on the database. In actual fact from trial and error, I have identified multiple queries are being called.

For example, if you run the following query (This query identifies all Item Ledger Entries, where the timestamp is odd):

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT TOP 1000 [Item No_]
	,[Posting Date]
	,[Description]
FROM [dbo].[CRONUS UK Ltd_$Item Ledger Entry]
WHERE ([timestamp] % 2) <> 0

Notice, that the transaction isolation level is READ UNCOMMITTED. This means that the query will read the dirty data and not block other users. Also it is limited to 1000 rows.

However when the task is running, it appears that the following SQL queries are run against the database:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT TOP 1000 [Item No_]
	,[Posting Date]
	,[Description]
FROM [dbo].[CRONUS UK Ltd_$Item Ledger Entry]
WHERE ([timestamp] % 2) <> 0

and

SELECT COUNT(*)
FROM [dbo].[CRONUS UK Ltd_$Item Ledger Entry]
WHERE ([timestamp] % 2) <> 0

The first query returns the data, the 2nd query returns the number of rows.

Notice in the 2nd query, the transaction isolation level is not declared… (In this case, will cause blocking). Also the TOP statement has been removed too, meaning that all records are counted – not good especially when there is a complex filter such as the above which can be time consuming.

Solution

A solution for this is to disable the counting of the records. One way I have identified how to do this is to UNION to a table returning no rows. (As obviously you don’t want to return rows to corrupt your SQL query).

In this example:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT TOP 1000 [Item No_]
	,[Posting Date]
	,[Description]
FROM [dbo].[CRONUS UK Ltd_$Item Ledger Entry]
WHERE ([timestamp] % 2) <> 0
UNION
(SELECT NULL,NULL,NULL WHERE 1<>1)

I’ve identified that when UNION is used, Taskcentre does not count the number of records. This means that if you are using the data, the “RowCount” will return -1 instead of the actual data. If you need this for a further step (such as a decision), then you have to calculate it by looping round in VB as per this knowledgebase article.

Note and Disclaimer:
I don’t have knowledge of how the software actually works internally, so this document is only based on my experience and investigation, so is provided as-is.

Automatic T-SQL Formatting with Microsoft SQL Server Management Studio 2014

Within MySql Workbench, there’s a feature called “Clean Up SQL” which automatically tidies up your SQL and the indentation automatically.

This is a very useful feature, as your messy SQL then becomes nice and neat. Unfortunately with Microsoft SQL Server Management Studio (SSMS), this feature is not standard.

Poor Mans T-SQL Formatter - Format T-SQL CodeLuckily I came across the following open source T-SQL formatter library, complete with an add-in for SSMS which adds this functionality – Poor Mans T-SQL Formatter. This plugin automatically formats your SQL, adding tabs and linebreaks etc in order to make it easy to read.

After installing the product, the following folder needs renaming in order to work with the 2014 version of SSMS:

%SystemDrive%\ProgramData\Microsoft\SQL Server Management Studio\11.0

To:

%SystemDrive%\ProgramData\Microsoft\SQL Server Management Studio\12.0

This is due to the installer. The installer puts the files in the incorrect place for the 2014 version of SQL Management Studio. Once you have renamed the folder and restarted SSMS, it will automatically add the options to the Tools menu.
Poor Mans T-SQL Formatter File Changes

Not only does this plugin work for SQL Management Studio, there are also versions for:

If you don’t wish to install the software, there’s an online version available.

Estimate Completion Time For Long Running SQL Query

With Microsoft SQL Server, sometimes there can be long running SQL Queries which you would like to get an estimated ETA from.

Tim Ford has wrote a brilliant article which will provide an ETA for the following types of queries:

  • Database Backup
    • BACKUP
  • Database Restore
    • RESTORE
  • Index Reorganisation
    • DBREINDEX
  • DBCC Operations, such as:
    • SHRINKFILE
    • SHRINKDATABASE
    • CHECKDB
    • CHECKTABLE
  • Rollback Operations
    • KILLED/ROLLBACK

Running the following SQL query will give the following information for all transactions which are awaiting completion:

  • Session ID
  • Percentage Complete
  • Time Elapsed in Seconds
  • Wait Type
  • Wait Time
  • Last Wait Type
  • Estimated Completion Time
  • SQL Transaction Text
  • SQL Statement Currently Executing
SELECT R.session_id, 
R.percent_complete, 
R.total_elapsed_time/1000 AS elapsed_secs, 
R.wait_type,
R.wait_time,
R.last_wait_type,
DATEADD(s,100/((R.percent_complete)/ (R.total_elapsed_time/1000)), R.start_time) estim_completion_time,
ST.text, 
SUBSTRING(ST.text, R.statement_start_offset / 2, 
 (
 CASE WHEN R.statement_end_offset = -1 THEN DATALENGTH(ST.text)
 ELSE R.statement_end_offset
 END - R.statement_start_offset 
 ) / 2
) AS statement_executing
FROM sys.dm_exec_requests R
CROSS APPLY sys.dm_exec_sql_text(R.sql_handle) ST
WHERE R.percent_complete > 0
AND R.session_id <> @@spid
OPTION(RECOMPILE);