Split SQL script tool

csharp sql tables

I have created a simple C# console application a split SQL script tool to split big scripts. For example, I had a problem with data seed scripts. Those scripts are huge because the database has more than a million records. These records need to be added to a new database. Each script is roughly 400Mb and I can’t upload it in the repository in Azure DevOps for example.

Split SQL script tool - Example of big SQL to split
Example of big SQL to split

Also, I created a repository for the Split SQL script tool. It contains the code in C#. This repository is available on GitHub.

Split SQL script tool in action
Split SQL script tool in action

Implementation explained

For the implementation, I added 2 libraries:

I have to explore the possibility of using only Spectre.Console that has similar functionality to System.CommandLine.DragonFruit.

System.CommandLine.DragonFruit

So, this library extends the Main function of the console application. The documentation is available on the Microsoft documentation but it is old and the library is not maintained.

The magic occurs in the Main function and its attribute. For example, look at the code:

    /// <summary>
    /// Split SQL script in multiple files based on the required size.
    /// </summary>
    /// <param name="file">The SQL script file to split.</param>
    /// <param name="destination">The destination folder. If this is empty or null,
    /// the new files will be created in the same directory as the original file.</param>
    /// <param name="limit">The maximum bytes limit for the new files.</param>
    /// <param name="addGo">if set to <c>true</c> the procedure will add the command GO 
    /// at the end of each file.</param>
    public static void Main(string file, string? destination,
        int limit = 10240000, bool addGo = true)

Using this package, the application has the --help option and it is displayed in a professional way the details. Here the screenshot

Example of the help in the console - Split SQL script tool
Example of the help in the console

Options

OptionDescriptionDefault
fileThe SQL script file to split.
destinationThe destination folder. If this is empty or null, the new files will be created in the same directory as the original file.
limitThe maximum bytes limit for the new files. [default: 10240000]10240000
add-goIf set to true the procedure will add the command GO at the end of each file.True
versionShow version information
?, h, helpShow help and usage information

Wrap up

In conclusion, I hope that the code for a split SQL script tool will be useful for you. Please, use the code and send your PR to improve the project.

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.