Try this out so you can see where you have gone wrong (you can practice at
SQLFiddle[
^])
Create table test(
TestID int identity(1,1),
DT datetime,
col1 varchar(max),
col2 varchar(max),
col3 varchar(max)
)
insert into test (TestID, DT, col1, col2, col3) values(1,GETDATE(), 'a','B','X')
You will get an error
Quote:
Schema Creation Failed: Cannot insert explicit value for identity column in table 'test' when IDENTITY_INSERT is set to OFF.:
Now you may be tempted to set IDENTITY_INSERT to 'ON' - but see the comment from Wes Aday ... what would be the point of having that
identity
column in the first place??
Allow SQL to automatically do that for you!
insert into test (DT, col1, col2, col3) values(GETDATE(), 'a','B','X')
In fact, if you are providing data for all of the columns
except the identity column then you don't even need the list of columns.
insert into test values(GETDATE(), 'a','B','X')
works just as well, I get the following
1 July, 16 2014 12:19:24+0000 a B X
2 July, 16 2014 12:19:24+0000 a B X
Point to Note: I changed that column name from
[datetime]
to
DT
. It is a very bad idea to use
SQL Reserved Words[
^] as column names... just because you can get away with it by surrounding the column name with square brackets [] doesn't mean that you should