Generated Script via SMO can't be executed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Generated Script via SMO can't be executed



I'm using Smo to create a SQL script of a existing database. For testing purposes I deleted my database and copied the script into a new query to create it again.
Unfortunately, this process produces an error.


'CREATE VIEW' must be the first statement in a query batch



Since I'm creating several views, this error is displayed several times on my messages tab in SQL Server Management Studio.
When I looked for solutions I found out that the keyword "Go" before "Create View" was apparently missing.
Is an option included in the class "ScriptingOptions" to append "GO" in the generated script?



Code:


public void GenerateSQLScripts(string dbName)


StringBuilder sb = new StringBuilder();
Server server = new Server(SqlServer);
Database db = server.Databases[dbName];


var scriptopt = new ScriptingOptions();
scriptopt.TargetServerVersion = SqlServerVersion.Version105; // Windows 2008 R2
scriptopt.AnsiPadding = true;
scriptopt.WithDependencies = true;
scriptopt.IncludeHeaders = true;
scriptopt.SchemaQualify = true;
scriptopt.ExtendedProperties = true;
scriptopt.TargetDatabaseEngineType = DatabaseEngineType.Standalone;
scriptopt.IncludeDatabaseContext = true;
scriptopt.ScriptDrops = false;
scriptopt.ScriptData = false;
scriptopt.ScriptSchema = true;
scriptopt.DriAllConstraints = true;
scriptopt.DriForeignKeys = true;
scriptopt.Indexes = true;
scriptopt.DriPrimaryKey = true;
scriptopt.DriUniqueKeys = true;
scriptopt.DriChecks = true;
scriptopt.AllowSystemObjects = false;
scriptopt.AppendToFile = false;

// script Tables
foreach (Table t in db.Tables)

if (!t.IsSystemObject)

StringCollection sc = t.Script(scriptopt);
foreach (string s in sc)

sb.AppendLine(s);





//Script Stored Procedures
foreach (StoredProcedure sp in db.StoredProcedures)

if (!sp.IsSystemObject)

var sc = sp.Script(scriptopt);
foreach (string s in sc)

sb.AppendLine(s);





//Views
foreach(View v in db.Views)
if (!v.IsSystemObject)

StringCollection sc = v.Script(scriptopt);
foreach (string s in sc)

sb.AppendLine(s);




File.WriteAllText(Path, sb.ToString());






Actually GO is NOT a keyword. It is the default batch separator in SSMS. It can be replaced with just about anything. Not sure how to have SMO include batch separators in the generated script.
– Sean Lange
Aug 6 at 15:15





@SeanLange Didn't know that, was referring to this question: stackoverflow.com/questions/13340332/…
– Scorch
Aug 6 at 15:19





Did you look at the documentation? Seems like scriptbatchterminator does exactly this.
– SMor
Aug 6 at 15:29





@SMor Thanks for the advice, but I got the same error again. The script also has the same number of lines as before the activation of the option.
– Scorch
Aug 7 at 12:19









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered