기본값 생성, 변경 및 제거

SQL Server Management Objects(SMO)에서 기본 제약 조건은 Default 개체로 표시됩니다.

Default 개체의 TextBody 속성은 삽입할 값을 설정하는 데 사용되며, 상수 또는 상수 값을 반환하는 GETDATE()와 같은 Transact-SQL 문일 수 있습니다. 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 AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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 AdventureWorks2012 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 AdventureWorks2012 database. 
            Database  db = srv.Databases["AdventureWorks2012"];

            //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 AdventureWorks2012
            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 AdventureWorks2012
CD \sql\localhost\default\databases
$db = get-item Adventureworks2012

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

참고 항목

참조

Default