Skip to content

Instantly share code, notes, and snippets.

@benhysell
Created June 13, 2019 14:59
Show Gist options
  • Save benhysell/7220c325488b8c495e3207d652621850 to your computer and use it in GitHub Desktop.
Save benhysell/7220c325488b8c495e3207d652621850 to your computer and use it in GitHub Desktop.
Extract Data From MS SQL via mssql-scripter
#https://github.com/Microsoft/mssql-scripter
#step 1 run this script to pull out all data to a local directory
#step 2 run result files through GenerateScripts application to strip out date time for system controlled fields
#step 3 save / reinsert using standard script
mssql-scripter -S localhost -d databaseName -P PASSWORD --data-only --include-objects dbo.TableName > './001 - TableName.sql'
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace DatabaseGenerateScripts
{
//cleans up the scripts generated from mssql-scripter
class Program
{
static void Main(string[] args)
{
var regex = new Regex(@"^(.+?),(?<secondToLast>[^,]+?),(?<last>[^,]*)$");
var directoryIn = @"C:\Users\benh\temp\in";
var directoryOut = @"C:\Users\benh\temp\out";
foreach (var file in Directory.GetFiles(directoryIn))
{
using (var readStream = new StreamReader(file))
using (var writeStream = new StreamWriter(Path.Combine(directoryOut, Path.GetFileName(file))))
{
var line = string.Empty;
//skip first two lines
readStream.ReadLine();
readStream.ReadLine();
while ((line = readStream.ReadLine()) != null)
{
var match = regex.Match(line);
if (match.Success)
{
writeStream.WriteLine($"{match.Groups[1]}, default, default)");
}
}
writeStream.WriteLine("GO");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment