Showing posts with label constraint. Show all posts
Showing posts with label constraint. Show all posts

Thursday, March 29, 2012

Failed to script out Dropping Default Constraint script for table columns

I used the following code to script out Creating default constraints. It works fine. But if I change so.ScriptDrops = true to script Dropping default constraints, it always fails with

Exception: {"Script failed for DefaultConstraint 'DF_Employee_VacationHours'. "}

InnerException: {"Object reference not set to an instance of an object."}

Can anyone shred some light on it? Thanks in advance.

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Text;

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;

namespace Test2

{

class Program

{

static void Main(string[] args)

{

Server s = new Server(".");

StringCollection sc;

ScriptingOptions so=new ScriptingOptions();

so.ScriptDrops = false;

s.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;

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

{

foreach (Column c in t.Columns)

{

if (c.DefaultConstraint!=null)

{

Console.WriteLine(string.Format("{0},{1}", t.Name, c.Name));

sc=c.DefaultConstraint.Script(so);

foreach (string st in sc)

{

Console.WriteLine(string.Format("{0}", st));

}

sc = null;

}

}

}

return;

}

}

}

Hi,

Thank you for reporting this issue. This is a defect in SMO, and we're filing a bug report. We will try to fix it for the next release of the product.