How do you parse large SQL scripts in batches?

I have a very large sql file that I want to split into batches for execution. I want to make sure that I parse it the same way as SSMS and SQLCMD.

Microsoft has an excellent build in mixed mode called Microsoft.SqlServer.BatchParser with the Parser class, which seams as if it would be a trick.

He wants to implement IBatchSource as an argument to SetBatchSource before calling Parse ().

Where can I find an implementation of IBatchSource and additional information on how to use this functionality?

+5
source share
1 answer

Microsoft.SqlServer.BatchParser GAC Microsoft.SqlServer.BatchParserClient, IBatchSource.

namespace Microsoft.SqlServer.Management.Common
{
  internal class BatchSourceFile : IBatchSource
  internal class BatchSourceString : IBatchSource
}

.

: ! Microsoft.SqlServer.Management.Common.ExecuteBatch. StringCollection GetStatements ( sqlCommand)?

Me: , , BatchParserClient assembly. , !

( !)

  • Microsoft SQL Server 2008 R2
  • Microsoft.SqlServer.BatchParser.dll Microsoft.SqlServer.BatchParserClient.dll GAC .
  • Microsoft.SqlServer.BatchParser Microsoft.SqlServer.BatchParserClient

Program.cs

using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using Microsoft.SqlServer.Management.Common;

namespace ScriptParser
{
   class Program
   {
      static void Main(string[] args)
      {
         ExecuteBatch batcher = new ExecuteBatch();
         string text = File.ReadAllText(@"Path_To_My_Long_Sql_File.sql");
         StringCollection statements = batcher.GetStatements(text);
         foreach (string statement in statements)
         {
            Console.WriteLine(statement);
         }
      }
   }
}

App.Config

<?xml version="1.0"?>
<configuration>
   <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
   </startup>
</configuration>

- ScriptDom, : fooobar.com/questions/1154790/....

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.SqlServer.TransactSql.ScriptDom;

namespace ScriptDomDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            TSql120Parser parser = new TSql120Parser(false);
            IList<ParseError> errors;
            using (StringReader sr = new StringReader(@"create table t1 (c1 int primary key)
GO
create table t2 (c1 int primary key)"))
            {
                TSqlFragment fragment = parser.Parse(sr, out errors);
                IEnumerable<string> batches = GetBatches(fragment);
                foreach (var batch in batches)
                {
                    Console.WriteLine(batch);
                }
            }
        }

        private static IEnumerable<string> GetBatches(TSqlFragment fragment)
        {
            Sql120ScriptGenerator sg = new Sql120ScriptGenerator();
            TSqlScript script = fragment as TSqlScript;
            if (script != null)
            {
                foreach (var batch in script.Batches)
                {
                    yield return ScriptFragment(sg, batch);
                }
            }
            else
            {
                // TSqlFragment is a TSqlBatch or a TSqlStatement
                yield return ScriptFragment(sg, fragment);
            }
        }

        private static string ScriptFragment(SqlScriptGenerator sg, TSqlFragment fragment)
        {
            string resultString;
            sg.GenerateScript(fragment, out resultString);
            return resultString;
        }
    }
}
+12

All Articles