Mark
Mark

Reputation: 315

Insert current date into ASP SQL

have a table with a transaction date in it. It is set as a timestamp and basically I want on creation of a new record that field automatically inserts the current date. I am using visual studio 2010 and ASP.net SQL not sure how to go about it. Dont need SQL injection protection for this just a simple way of doing it. Any ideas?

Mark

Upvotes: 0

Views: 5683

Answers (3)

Rob
Rob

Reputation: 1328

INSERT INTO [AdventureWorks].[dbo].[ErrorLog]
           ([ErrorTime]
           ,[UserName]
           ,[ErrorNumber]
           ,[ErrorMessage]
)
     VALUES
           (GetDate() --This will work for MSSQL
           ,'userName'
           ,7551
           ,'I inserted the current date/Time'
)

or (again for SQL server)

ALTER TABLE dbo.ErrorLog ADD CONSTRAINT
    DF_ErrorLog_ErrorTime_current_DateTime DEFAULT (getdate()) FOR ErrorTime)

Upvotes: 3

sehe
sehe

Reputation: 392999

  • Oracle has a SYSDATE() function,
  • SqlServer has GETDATE()
  • MySQL has NOW()

Use it in your DML (SELECT, INSERT) statements

Upvotes: 3

RichO
RichO

Reputation: 733

If the above code is in a stored procedure, then getDate() should work. If it is a sql command string that you are sending via ado.net, then you need to do this (example in VB):

sqlString="Insert into myTable (myDateTimeColumn) Values ('" & now() & "')"

myCommand.Execute(sqlString) ... etc.

Upvotes: 0

Related Questions