Created
June 13, 2019 14:59
-
-
Save benhysell/7220c325488b8c495e3207d652621850 to your computer and use it in GitHub Desktop.
Extract Data From MS SQL via mssql-scripter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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