SMO – Finding check constraints referencing a specified column

As part of a schema comparison program my company has, we needed to be able to identify check constraints that reference a specified column.
This method takes a SMO Table object, and the name of a column in that table.

// Assumes that column names within constraint text are bracketed.
public static List GetCheckConstraintsForColumn(
Table table,
string columnName
)
{
if (!columnName.StartsWith(“[“))
{
columnName = “[” + columnName + “]”;
}
List checkConstraintList = new List();
foreach (Check checkConstraint in table.Checks)
{
if (checkConstraint.Text.Contains(columnName))
{
checkConstraintList.Add(checkConstraint);
}
}
return checkConstraintList;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: