Compartir a través de


Crear, modificar y eliminar valores predeterminados

En los objetos de administración de SQL Server (SMO), el objeto Default representa la restricción predeterminada.

La propiedad TextBody del objeto Default se utiliza para definir el valor que se insertará. Puede ser una constante o una instrucción Transact-SQL que devuelve un valor constante, como GETDATE(). La propiedad TextBody no puede modificarse mediante el método Alter. En lugar de ello, debe quitarse el objeto Default y volver a crearse.

Ejemplo

Para utilizar cualquier ejemplo de código que se proporcione, deberá elegir el entorno de programación, la plantilla de programación y el lenguaje de programación en los que crear su aplicación. Para obtener más información, vea Cómo crear un proyecto de Visual Basic SMO en Visual Studio .NET o Cómo crear un proyecto de Visual C# SMO en Visual Studio .NET.

Crear, modificar y quitar un valor predeterminado en Visual Basic

En este ejemplo de código se muestra cómo crear un valor predeterminado que está formado por texto simple y otro valor predeterminado que es una instrucción Transact-SQL. El valor predeterminado debe adjuntarse a la columna mediante el método BindToColumn y debe desasociarse mediante el método 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()

Crear, modificar y quitar un valor predeterminado en Visual C#

En este ejemplo de código se muestra cómo crear un valor predeterminado que está formado por texto simple y otro valor predeterminado que es una instrucción Transact-SQL. El valor predeterminado debe adjuntarse a la columna mediante el método BindToColumn y desasociarse utilizando el método 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();
        }

Crear, modificar y quitar un valor predeterminado en PowerShell

En este ejemplo de código se muestra cómo crear un valor predeterminado que está formado por texto simple y otro valor predeterminado que es una instrucción Transact-SQL. El valor predeterminado debe adjuntarse a la columna mediante el método BindToColumn y desasociarse utilizando el método 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()

Vea también

Referencia