将 DateTime 数据库字段设置为“ Now”

在 VB. net 代码中,我使用 SQL 参数创建请求。如果我将 DateTime 参数设置为值 DateTime。现在,我的请求是什么样的?

UPDATE table SET date = "2010/12/20 10:25:00";

或者

UPDATE table SET date = GETDATE();

在第一种情况下,我确信每个记录将被设置为完全相同的时间。在第二种情况下,它取决于 DBMS 如何处理请求。这就引出了第二个问题: SQLServer 在使用 NOW ()更新大型表时是否设置了相同的日期和时间?

编辑: 由 GETDATE ()替换 NOW ()(在 SQL Server 中不存在)。

334540 次浏览

In SQL you need to use GETDATE():

UPDATE table SET date = GETDATE();

There is no NOW() function.


To answer your question:

In a large table, since the function is evaluated for each row, you will end up getting different values for the updated field.

So, if your requirement is to set it all to the same date I would do something like this (untested):

DECLARE @currDate DATETIME;
SET @currDate = GETDATE();


UPDATE table SET date = @currDate;

Use GETDATE()

Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.

UPDATE table SET date = GETDATE()

An alternative to GETDATE() is CURRENT_TIMESTAMP. Does the exact same thing.