Monday, March 12, 2012

I am scripting drop PRIMARY KEY script, but got DROP TABLE script

I am scripting the drop PRIMARY KEY script for all tables. But the script I got was DROP TABLE. Script options are as following:

so.DriUniqueKeys = false;

so.DriDefaults = false;

so.DriChecks = false;

so.Indexes = false;

so.DriForeignKeys = false;

so.PrimaryObject = false;

so.DriPrimaryKey = true;

so.IncludeIfNotExists = true;

so.ScriptDrops = true;

The create PRIMARY KEY script is right if I change so.ScriptDrops to false.

Am i doing something wrong or is it a bug? How to script out drop PK script?

hard to tell without the rest of your script, but what about that one here:

Server s = new Server(".");

s.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;

foreach (Table t in s.Databases["Adventureworks"].Tables)

{

foreach (Index i in t.Indexes)

{

if (i.IndexKeyType == IndexKeyType.DriPrimaryKey)

{

i.Drop();

break;

}

}

}

s.Alter();

foreach (string st in s.ConnectionContext.CapturedSql.Text)

{

Console.WriteLine(st);

Console.ReadLine();

}

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

Thanks Jens, It works great.

Based on my test, the DriPrimaryKey=true only works for create script for tables. DriPrimaryObject=false only works for create script for tables too. For drop script,smo always generates drop table script. That's why I got drop table script when i tried to generate drop PK script (DriPrimaryKey=true and

DriPrimaryObject=false ).

The BOL lacks details about the scripting options.

No comments:

Post a Comment