SQLTeam.com | Weblogs | Forums

Insert statement: Auto Increment with Version Number and Datetime


#1

Hi Friends,

I have a table with 2 columns Version and CurrentDateTime

For every insert the Records should be Autoincrement for Version Column and Record Insertdatetime for that Version
1.0 | 2015-11-12 10:01.23..
2.0 | 2015-11-12 10.02.11..
............................


#2

Hi,

You can set identity for version like identity(1,1) and default value for datetime column like getdate() along with other columns.
Make sure you have more than 2 columns in your table because u can't automate all the columns except using triggers.

Thanks
Subha


#3

Can you just give me the query ?


#4

What is your table definition? As @subhaoviya said you need more than two columns.

And which version of SQL Server are you using?


#5
create table tbl
(
    Version        int identity(1,1),
    CurrentDateTime    datetime default getdate()
)

insert into tbl values (default)
insert into tbl values (default)

select    *
from    tbl

drop table tbl