200

我有一张表,其中一列是日期时间类型的“日期”。我们决定为该列添加一个默认约束

Alter table TableName
alter column dbo.TableName.Date default getutcdate() 

但这给了我错误:

'.' 附近的语法不正确

有没有人在这里看到任何明显错误的东西,我错过了(除了为该列提供更好的名称)

4

7 回答 7

370

尝试这个

alter table TableName 
 add constraint df_ConstraintNAme 
 default getutcdate() for [Date]

例子

create table bla (id int)

alter table bla add constraint dt_bla default 1 for id



insert bla default values

select * from bla

还要确保您命名默认约束..稍后删除它会很痛苦,因为它将具有那些疯狂的系统生成的名称之一......另请参阅如何命名默认约束以及如何删除默认约束SQL Server 中的名称

于 2010-01-19T17:07:56.303 回答
7

我使用下面的存储过程来更新列的默认值。

在添加新的默认值之前,它会自动删除列上的任何先前默认值。

使用示例:

-- Update default to be a date.
exec [dbo].[AlterDefaultForColumn] '[dbo].[TableName]','Column','getdate()';
-- Update default to be a number.
exec [dbo].[AlterDefaultForColumn] '[dbo].[TableName]','Column,'6';
-- Update default to be a string. Note extra quotes, as this is not a function.
exec [dbo].[AlterDefaultForColumn] '[dbo].[TableName]','Column','''MyString''';

存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- Sample function calls:
--exec [dbo].[AlterDefaultForColumn] '[dbo].[TableName]','ColumnName','getdate()';
--exec [dbol].[AlterDefaultForColumn] '[dbo].[TableName]','Column,'6';
--exec [dbo].[AlterDefaultForColumn] '[dbo].[TableName]','Column','''MyString''';
create PROCEDURE [dbo].[ColumnDefaultUpdate]
    (
        -- Table name, including schema, e.g. '[dbo].[TableName]'
        @TABLE_NAME VARCHAR(100), 
        -- Column name, e.g. 'ColumnName'.
        @COLUMN_NAME VARCHAR(100),
        -- New default, e.g. '''MyDefault''' or 'getdate()'
        -- Note that if you want to set it to a string constant, the contents
        -- must be surrounded by extra quotes, e.g. '''MyConstant''' not 'MyConstant'
        @NEW_DEFAULT VARCHAR(100)
    )
AS 
BEGIN       
    -- Trim angle brackets so things work even if they are included.
    set @COLUMN_NAME = REPLACE(@COLUMN_NAME, '[', '')
    set @COLUMN_NAME = REPLACE(@COLUMN_NAME, ']', '')

    print 'Table name: ' + @TABLE_NAME;
    print 'Column name: ' + @COLUMN_NAME;
    DECLARE @ObjectName NVARCHAR(100)
    SELECT @ObjectName = OBJECT_NAME([default_object_id]) FROM SYS.COLUMNS
    WHERE [object_id] = OBJECT_ID(@TABLE_NAME) AND [name] = @COLUMN_NAME;

    IF @ObjectName <> '' 
    begin
        print 'Removed default: ' + @ObjectName;
        --print('ALTER TABLE ' + @TABLE_NAME + ' DROP CONSTRAINT ' + @ObjectName)
        EXEC('ALTER TABLE ' + @TABLE_NAME + ' DROP CONSTRAINT ' + @ObjectName)
    end

    EXEC('ALTER TABLE ' + @TABLE_NAME + ' ADD  DEFAULT (' + @NEW_DEFAULT + ') FOR ' + @COLUMN_NAME)
    --print('ALTER TABLE ' + @TABLE_NAME + ' ADD  DEFAULT (' + @NEW_DEFAULT + ') FOR ' + @COLUMN_NAME)
    print 'Added default of: ' + @NEW_DEFAULT;
END

此存储过程消除的错误

如果您尝试在已经存在的列中添加默认值,您将收到以下错误(如果使用此存储过程,您将永远不会看到):

-- Using the stored procedure eliminates this error:
Msg 1781, Level 16, State 1, Line 1
Column already has a DEFAULT bound to it.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
于 2014-04-09T12:15:11.507 回答
7

您可以将保留字括在方括号中以避免这些类型的错误:

dbo.TableName.[Date]
于 2010-01-19T17:04:21.740 回答
6

实际上你必须像下面的例子那样做,这将有助于解决问题......

drop table ABC_table

create table ABC_table
(
    names varchar(20),
    age int
)

ALTER TABLE ABC_table
ADD CONSTRAINT MyConstraintName
DEFAULT 'This is not NULL' FOR names

insert into ABC(age) values(10)

select * from ABC
于 2014-01-28T08:39:30.220 回答
0

我像 JohnH 的评论一样确认,永远不要在您的对象名称中使用列类型!这很令人困惑。并尽可能使用括号。

尝试这个:

ALTER TABLE [TableName]
ADD  DEFAULT (getutcdate()) FOR [Date]; 
于 2021-02-11T11:05:41.743 回答
0

您指定了两次表名。ALTER TABLE 部分命名表。尝试:Alter table TableName alter column [Date] default getutcdate()

于 2018-09-14T14:06:03.070 回答
0

更改表 TableName 删除约束 DF_TableName_WhenEntered

alter table TableName 添加约束 DF_TableName_WhenEntered default getutcdate() for WhenEntered

于 2019-06-22T09:51:23.730 回答