开发者

Automatic Adding Current DateTime In TableField

I am using SQL SERVER 2005 and i am also newbie to SQL SERVER

now i need to know that is there any way or any technique in SQL SERVER 2005

such that as soon as i add new record in table then current date-time should be a开发者_如何学JAVAdded in to any given field of table.

Example:

Suppose i have CUSTOMER table and it has fields say CustomerID,CustomerName,....,DateTime. now whenever new customer added in this table then current date-time should be automatically added in to DateTime Field of CUSTOMER table.


In SSMS one can set the Default value or binding property of the appropriate column of the table property to getdate().


You need to add default constraint:

alter table MyTable add constraint MyColumnDefault default getdate() for MyColumn;


I'm not much of an expert in SQL but you could use TIMESTAMP for this, see:
http://msdn.microsoft.com/en-us/library/ms182776%28v=sql.90%29.aspx


It sounds like you should have a look at the timestamp data type:

http://msdn.microsoft.com/en-us/library/ms182776%28v=sql.90%29.aspx


Check table definition with default value


Declare @Table Table 
(
    Id int identity,[Name] varchar(100),CreatedDate DateTime default (Getdate())
)
insert into @Table([Name]) 
values ('yogesh')
insert into @Table ([Name]) 
values ('Bhadauriya')
insert into @Table ([Name]) 
values ('Yogesh Bhadauriya')

select *
From @Table
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜