2010-01-19 170 views
146

我有一個表,其中一列是「日期」類型的日期時間。我們決定默認約束添加到列改變列,添加默認約束

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

,但是這給了我錯誤:

Incorrect syntax near '.'

有誰看到什麼明顯的錯誤在這裏,我很想念(除有一個更好的名字列)

+9

不要使用類型或關鍵字作爲列名! – JonH 2010-01-19 17:04:40

+4

yup,agree-「有沒有人在這裏看到任何明顯的錯誤,我錯過了(除了有一個更好的名字的列)」 – ram 2010-01-19 17:22:18

回答

279

試試這個

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 

還要確保你的名字默認constraint..it將在頸部疼痛後放棄它,因爲它會那些瘋狂的系統的一個所產生的名字......還看到How To Name Default Constraints And How To Drop Default Constraint Without A Name In SQL Server

6

你可以在方括號包裹保留字,以避免此類錯誤:

dbo.TableName.[Date] 
+0

這似乎是一個非常糟糕的主意,使用列名保留字。 – 2017-09-27 12:44:02

5

其實你要做的就像下面的例子,這將有助於解決這一問題......

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

我使用下面的存儲過程更新列上的默認值。

在添加新的默認值之前,它會自動刪除列上的所有先前默認值。用法

例子:

-- 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.