Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 973539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:17:40+00:00 2026-05-16T03:17:40+00:00

I’m currently using SMO to help automate the patching and/or upgrading of customer databases

  • 0

I’m currently using SMO to help automate the patching and/or upgrading of customer databases that are in production. I’m using the following code

Public Sub RunScript(ByVal db As Database, ByVal scriptInfo As FileInfo)
    If scriptInfo.Exists Then

        RaiseEvent LogMessage("Executing Indicated Script: " & scriptInfo.Name)
        Using sr As New StreamReader(scriptInfo.FullName)
            Dim script As String = sr.ReadToEnd
            Try
                db.ExecuteNonQuery(script)
            Catch ex As Exception
                RaiseEvent LogMessage("ERROR: " & ex.Message.ToString)
                db.ExecuteNonQuery(script,
                                Microsoft.SqlServer.Management.Common.ExecutionTypes.ContinueOnError)
            End Try
        End Using

    End If
End Sub

What I’m doing is logging the SQLConnection messages that are coming off of the SQLConneciton that I am using with the Database part that give me fairly simplistic messages about what is happening. Am I’m also Logging myself various messages through the LogMessage(string) event that I have created.

What I would really like to be able to do is execute the scripts so that the continue if there is an error, but still log that there is an error without having to do it the way I am now.

The reason is some of the errors that break the transactions are trivial and it’s okay that they are happening. Like trying to create an index when one exists or what-have-you.

Is there any way to both continue script execution on error but still log the error message?

**Edit **
A Clarification for what the two objects coming in are

The scriptInfo is a FileInfo object that tells the StreamReader where to load the sql script from.

The db is a SMO Database object that is associated with a SQLClient.Connection that allows me to execute the script on the database that is associated with db

The error logging is happening on the SqlClient.SqlConnections OnInfo Event, or from the LogMessage event

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:17:41+00:00Added an answer on May 16, 2026 at 3:17 am

    Assuming you are using SQL Server 2005 or above, you could use distinct TRY/CATCH blocks for every statement in the script. Here is an example:

    DECLARE @errorLog TABLE (
      row_id INT NOT NULL IDENTITY(1,1), 
      error_msg NVARCHAR(4000)
    ) 
    
    /** Create the table **/ 
    BEGIN TRY 
      CREATE TABLE t1 (RowID INT NOT NULL) 
    END TRY 
    
    BEGIN CATCH 
      INSERT @errorLog (error_msg) 
      SELECT ERROR_MESSAGE()
    END CATCH 
    
    /** Create the table again - will raise an error message **/ 
    BEGIN TRY 
      CREATE TABLE t1 (RowID INT NOT NULL) 
    END TRY 
    
    BEGIN CATCH 
      INSERT @errorLog (error_msg) 
      SELECT ERROR_MESSAGE()
    END CATCH 
    
    /** Create the index **/ 
    BEGIN TRY 
      CREATE INDEX IX_t1 ON t1 (RowID) 
    END TRY 
    
    BEGIN CATCH 
      INSERT @errorLog (error_msg)
      SELECT ERROR_MESSAGE()
    END CATCH 
    
    /** Return the errors **/
    IF EXISTS (SELECT * FROM @errorLog) 
      SELECT error_msg FROM @errorLog
    
    /** Confirm it worked **/ 
    EXEC sp_help 't1' 
    

    This script will continue processing after failures. If errors exist, they will be returned in a resultset. So you would need to alter your original code, but I think you might have to anyway in order to accomodate multiple errors within one script.

    On the flip side, things like duplicate index errors could easily be handled in the script itself (i.e. check to see if the object already exists). And since this solution requires an extensive amount of changes to the original script, that might be a preferable choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.