sp_bindefault (Transact-SQL)
Binds a default to a column or to an alias data type.
![]() |
---|
This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. We recommend that you create default definitions by using the DEFAULT keyword of the ALTER TABLE or CREATE TABLE statements instead. For more information, see Creating and Modifying DEFAULT Definitions. |
You can use sp_bindefault to bind a new default to a column, although using the DEFAULT constraint is preferred, or to an alias data type without unbinding an existing default. The old default is overridden. You cannot bind a default to a SQL Server system data type or a CLR user-defined type. If the default is not compatible with the column to which you have bound it, the SQL Server Database Engine returns an error message when it tries to insert the default value, not when you bind it.
Existing columns of the alias data type inherit the new default, unless either a default is bound directly to them or futureonly_flag is specified as futureonly. New columns of the alias data type always inherit the default.
When you bind a default to a column, related information is added to the sys.columns catalog view. When you bind a default to an alias data type, related information is added to the sys.types catalog view.
A. Binding a default to a column
A default named today has been defined in the current database by using CREATE DEFAULT, the following example binds the default to the HireDate column of the Employee table. Whenever a row is added to the Employee table and data for the HireDate column is not supplied, the column gets the value of the default today.
USE master; GO EXEC sp_bindefault 'today', 'HumanResources.Employee.HireDate';
B. Binding a default to an alias data type
A default named def_ssn and an alias data type named ssn already exists. The following example binds the default def_ssn to ssn. When a table is created, the default is inherited by all columns that are assigned the alias data type ssn. Existing columns of type ssn also inherit the default def_ssn, unless futureonly is specified for futureonly_flag value, or unless the column has a default bound directly to it. Defaults bound to columns always take precedence over those bound to data types.
USE master; GO EXEC sp_bindefault 'def_ssn', 'ssn';
C. Using the futureonly_flag
The following example binds the default def_ssn to the alias data type ssn. Because futureonly is specified, no existing columns of type ssn are affected.
USE master; GO EXEC sp_bindefault 'def_ssn', 'ssn', 'futureonly';
D. Using delimited identifiers
The following example shows using delimited identifiers, [t.1], in object_name.
USE master; GO CREATE TABLE [t.1] (c1 int) -- Notice the period as part of the table name. EXEC sp_bindefault 'default1', '[t.1].c1' -- The object contains two periods; -- the first is part of the table name, -- and the second distinguishes the table name from the column name.