venerdì 13 gennaio 2012

Powershell and SQL "GO" separator

When executing a SQL statement in Powershell that contains words like "GO" it generates the error:

"Incorrect syntax near 'GO' "

The explanation stands in the fact that GO is not a SQL keyword. It's a batch separator used by client tools (like SSMS) to break the entire script up into batches.
The solutions may be to break up the script into batches, with a script like this:

private static List getCommands(string testDataSql)
{
string[] splitcommands = File.ReadAllText(testDataSql).Split(new string[]{"GO\r\n"}, StringSplitOptions.RemoveEmptyEntries);
List commandList = new List(splitcommands);
return commandList;
}