1

I want to catch progress messages when backing up my database as described here.

It works fine, but I worry about how the percentage done is extracted from the info messages. In the comments @ChristianHayter proposes a regex, which is language specific (in that case English).

Does sql server send info messages always in English or does it depend on the system settings. If so, how can I find out, what the format of the progess message is. I think I remember a table in sql server, which stores all those messages (something like "{0} percent complete"), but I can't remember the name.

Community
  • 1
  • 1
Sascha
  • 1,210
  • 1
  • 17
  • 33
  • Oh...hold on. It's sys.messages (SELECT * FROM master.sys.messages m WHERE text LIKE '%percent processe%') In that case I probably could check for the message Id...right? I'll try that and report later – Sascha Dec 29 '15 at 07:15

2 Answers2

1

You can also use

SELECT session_id as SPID, command, a.text AS Query, start_time, percent_complete, dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time 
FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a 
WHERE r.command in ('BACKUP DATABASE','RESTORE DATABASE')

This will be useful if someone kicks off the process from some other machine then you can use this to get the progress.

Does sql server send info messages always in English or does it depend on the system settings.

Not always English. It is the same which you selected while installing the SQL Server management studio. You can also change it via:

enter image description here

Also check Local Language Versions in SQL Server

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

As mentioned in my comment above I wanted to report, if using the message id works. It does, even though I assume that message ids never change. I think that is a valid assumption. As a second assumption I assume, that the progress message in whatever language will contain an inter number, that represents the progress.

Than this will work:

Private Sub _SqlConnection_InfoMessage(sender As Object, e As System.Data.SqlClient.SqlInfoMessageEventArgs) Handles _SqlConnection.InfoMessage
    If _BackupInProgress Then
        If e.Errors.Count > 0 Then
            For Each sqlError As SqlError In e.Errors
                If sqlError.Number = 3211 Then
                    Dim rxPercentageDone = New Regex("(\d*)")
                    Dim match = rxPercentageDone.Match(sqlError.Message)
                    If match.Success Then
                        ' match.Groups(1).Value holds the actual progress value
                        Debug.Print(e.Message)
                    End If
                End If
            Next
        End If
    End If
End Sub

You have to subscribe to the sql connection's InfoMessage-event. With the event handler above you can catch an process the message number 3211 and validate that it contains progress information with the regex.

That should be more robust that checking for language specific message texts.

Sascha
  • 1,210
  • 1
  • 17
  • 33