代码之家  ›  专栏  ›  技术社区  ›  ram

更改列,添加默认约束

  •  171
  • ram  · 技术社区  · 15 年前

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

    但这给了我一个错误:

    “.”附近的语法不正确

    5 回复  |  直到 8 年前
        1
  •  366
  •   SQLMenace    15 年前

    试试这个

    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
    

    How To Name Default Constraints And How To Drop Default Constraint Without A Name In SQL Server

        2
  •  7
  •   Peter Majeed    12 年前

    您可以将保留字用方括号括起来,以避免出现以下错误:

    dbo.TableName.[Date]
    
        3
  •  7
  •   Contango    11 年前

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

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

    -- 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.
    
        4
  •  5
  •   Baby Groot Duleendra    11 年前

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

    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
    
        5
  •  0
  •   Kelvin Lush    6 年前

    您指定了两次表名。ALTER TABLE部分为表命名。 尝试: 更改表表名 alter column[Date]默认getutcdate()

        6
  •  0
  •   Abhijit Poojari    6 年前

    删除约束DF_TableName_

    在输入时添加约束DF_TableName_ 输入时的默认getutcdate()

        7
  •  0
  •   SQLBlogger    4 年前

    这令人困惑。如果可能,请使用括号。

    试试这个:

    ALTER TABLE [TableName]
    ADD  DEFAULT (getutcdate()) FOR [Date];