Creating, Altering, and Removing Defaults
In SQL Server Management Objects (SMO), the default constraint is represented by the Default object.
The TextBody property of the Default object is used to set the value to be inserted. This can be a constant or a Transact-SQL statement that returns a constant value, such as GETDATE(). The TextBody property cannot be modified by using the Alter method. Instead, the Default object must be dropped and re-created.
To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see How to: Create a Visual Basic SMO Project in Visual Studio .NET or How to: Create a Visual C# SMO Project in Visual Studio .NET.
This code example shows how to create one default that is simple text, and another default that is a Transact-SQL statement. The default must be attached to the column by using the BindToColumn method and detached by using the UnbindFromColumn method.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a Default object variable by supplying the parent database and the default name
'in the constructor.
Dim def As [Default]
def = New [Default](db, "Test_Default2")
'Set the TextHeader and TextBody properties that define the default.
def.TextHeader = "CREATE DEFAULT [Test_Default2] AS"
def.TextBody = "GetDate()"
'Create the default on the instance of SQL Server.
def.Create()
'Declare a Column object variable and reference a column in the AdventureWorks database.
Dim col As Column
col = db.Tables("SpecialOffer", "Sales").Columns("StartDate")
'Bind the default to the column.
def.BindToColumn("SpecialOffer", "StartDate", "Sales")
'Unbind the default from the column and remove it from the database.
def.UnbindFromColumn("SpecialOffer", "StartDate", "Sales")
def.Drop()
This code example shows how to create one default that is simple text, and another default that is a Transact-SQL statement. The default must be attached to the column by using the BindToColumn method and detached by using the UnbindFromColumn method.
{
//Connect to the local, default instance of SQL Server.
{
Server srv = default(Server);
srv = new Server();
//Reference the AdventureWorks database.
Database db = default(Database);
db = srv.Databases("AdventureWorks");
//Define a Default object variable by supplying the parent database and the default name
//in the constructor.
Default def = default(Default);
def = new Default(db, "Test_Default2");
//Set the TextHeader and TextBody properties that define the default.
def.TextHeader = "CREATE DEFAULT [Test_Default2] AS";
def.TextBody = "GetDate()";
//Create the default on the instance of SQL Server.
def.Create();
//Declare a Column object variable and reference a column in the AdventureWorks database.
Column col = default(Column);
col = db.Tables("SpecialOffer", "Sales").Columns("StartDate");
//Bind the default to the column.
def.BindToColumn("SpecialOffer", "StartDate", "Sales");
//Unbind the default from the column and remove it from the database.
def.UnbindFromColumn("SpecialOffer", "StartDate", "Sales");
def.Drop();
}
