AndreaNobili AndreaNobili - 3 months ago 13
SQL Question

How can I insert the TimeStamp (current date and current time) into a datetime field of a Microsoft SQL server table?

I am not so into DB and I have the following problem trying to perform a simple insert query into a table in a Microsoft SQL Server database.

So, this table contains a

datetime
column named
TIMESTAMP
that have to contain the current date and the current time.

I am trying to do in this way but can't work:

INSERT INTO TirAliquotaRamoI (DataElaborazione,
TassoLordoAnnuoAppl,
TassoGiornaliereNetto,
Aliquota,
DataInizio,
DataFine,
TIMESTAMP)
VALUES ('2015-08-01 00:00:00',
0,
0,
1,
'2015-08-01 00:00:00',
'2100-12-32',
TIMESTAMP);


It give me the following error message:


16:49:10 [INSERT - 0 row(s), 0.000 secs]

Error Code: 207, SQL State: S0001
Invalid column name 'TIMESTAMP'.


Why? What am I missing? What is wrong? How can I fix this issue?

vkp vkp
Answer

You are inserting values in columns using values. So all of them have to be constant(s). They can't be column-names. Hence you get the error.

Change the query to insert GETDATE() (returns the current date with timestamp) into the TIMESTAMP column.

INSERT INTO TirAliquotaRamoI (DataElaborazione, 
                          TassoLordoAnnuoAppl,
                          TassoGiornaliereNetto, 
                          Aliquota, 
                          DataInizio, 
                          DataFine, 
                          TIMESTAMP)
VALUES ('2015-08-01 00:00:00',
    0,
    0,
    1,
    '2015-08-01 00:00:00',
    '2100-12-32',
    GETDATE());
Comments