共用方式為


建立、改變和移除預設值

在 SQL Server 管理物件 (SMO) 中,預設的條件約束是由 Default 物件表示。

Default 物件的 TextBody 屬性可用於設定要插入的値。這可以是常數,或傳回常數值的 Transact-SQL 陳述式,例如 GETDATE()。TextBody 屬性無法藉由 Alter 方法進行修改,反而必須將 Default 物件卸除,然後重新建立。

範例

如果要使用所提供的任何程式碼範例,您必須選擇建立應用程式用的程式設計環境、程式設計範本,及程式設計語言。如需詳細資訊,請參閱<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>或<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>。

在 Visual Basic 中建立、改變和移除預設值

此程式碼範例示範如何建立一個純文字的預設值,以及另一個 Transact-SQL 陳述式的預設值。預設值必須使用 BindToColumn 方法附加到資料行,並使用 UnbindFromColumn 方法卸離。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'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 AdventureWorks2008R2 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()

在 Visual C# 中建立、改變和移除預設值

此程式碼範例示範如何建立一個純文字的預設值,以及另一個 Transact-SQL 陳述式的預設值。預設值必須使用 BindToColumn 方法附加到資料行,並使用 UnbindFromColumn 方法卸離。

{
          
          Server srv = new Server();

            //Reference the AdventureWorks2008R2 database. 
            Database  db = srv.Databases["AdventureWorks2008R2"];

            //Define a Default object variable by supplying the parent database and the default name 
            //in the constructor. 
            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();
            
            //Bind the default to a column in a table in AdventureWorks2008R2
            def.BindToColumn("SpecialOffer", "StartDate", "Sales");

            //Unbind the default from the column and remove it from the database. 
            def.UnbindFromColumn("SpecialOffer", "StartDate", "Sales");
            def.Drop();
        }

在 PowerShell 中建立、改變和移除預設值

此程式碼範例示範如何建立一個純文字的預設值,以及另一個 Transact-SQL 陳述式的預設值。預設值必須使用 BindToColumn 方法附加到資料行,並使用 UnbindFromColumn 方法卸離。

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2008R2
CD \sql\localhost\default\databases
$db = get-item Adventureworks2008R2

#Define a Default object variable by supplying the parent database and the default name in the constructor.
$def = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Default `
-argumentlist $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()

#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()

請參閱

參考