views:

118

answers:

1

Using the Microsoft.SqlServer.Management.Smo.Scripter object, I'm generating scripts for each of the tables in my database, including indexes/constraints. When it generates the script, however, it includes all the index options, even though they are the default values, e.g.,

CREATE TABLE [dbo].[AgeGroup](
   [AgeGroupID] [int] NOT NULL),
CONSTRAINT [PK_AgeGroup] PRIMARY KEY CLUSTERED 
(
   [AgeGroupID] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
       ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],

All the options in the WITH clause of the primary key constraint are set to the default values (at least according to SQL Server Books Online). How can I get the Scripter object to not output those options?

Here is my code:

Server server = new Server("MYSERVER");

Scripter scripter = new Scripter(server);
scripter.Options.Indexes = true;
scripter.Options.AnsiPadding = true;
scripter.Options.AnsiFile = true;
scripter.Options.Permissions = true;
scripter.Options.ClusteredIndexes = true;
scripter.Options.DriAll = true;

Urn[] urns = new Urn[1];

Table t = server.Databases["MYDATABASE"].Tables["AgeGroup"];
urns[0] = t.Urn;

StringCollection sc = scripter.Script(urns);

using (StreamWriter sw = File.CreateText("AgeGroup.Table.sql"))
   foreach (string str in sc)
      sw.WriteLine(str);
+1  A: 

Apparently, there are no scripting Options to control that. There are more things like that in SMO.Scripting (i.e., uncontrolled) than most people realize. For instance, there is no option to script ALTER commands instead of CREATES, even though SSMS does it for the object quick-menu Modify item.

In most of these cases that I have seen, it turned out to be fairly easy to handle it in my code by scanning for and, if found, replacing the offending text. The scripter is very rigid in its output formatting, so I've never even needed RegEx's to do this. So, when you've got the CREATE script for a table object I would search for "*) WITH (PAD_INDEX*) ON " and replace it with " ON " (make sure that your wildcard will cross the line breaks).

RBarryYoung
This is the approach I ended up taking before I saw your answer. It is working well.
splattered bits