0

我在 ASPNETDB.MDF 中设置了一个名为“Today”的列,我只想在其中输入当前日期时间。我正在使用 getdate() 但它无法自动更新更新当前时间。假设用户在 2013 年 1 月 5 日晚上 9:24:08 创建了他/她的帐户,然后“今天”列显示 2013 年 1 月 5 日晚上 9:24:08,很好,但它不断设置,我想要' Today' 列将填充当前时间,例如 2013 年 1 月 7 日晚上 11:24:08,“今天”列在每行显示 2013 年 1 月 7 日晚上 11:24:08。

如果有人帮助我,我将不胜感激。

N:B: IMA BEGINNER,我认为这里有各种已解决的线程,我已经看过了,但很困惑哪一个适合我的问题。所以这个问题很无理取闹,我很抱歉。

    ALTER PROCEDURE dbo.aspnet_Membership_CreateUser
@ApplicationName                        nvarchar(256),
@UserName                               nvarchar(256),
@Password                               nvarchar(128),
@PasswordSalt                           nvarchar(128),
@Email                                  nvarchar(256),
@PasswordQuestion                       nvarchar(256),
@PasswordAnswer                         nvarchar(128),
@IsApproved                             bit,
@DaysLeft                               numeric(18, 0),
@CurrentTimeUtc                         datetime,
@CreateDate                             datetime = NULL,
@ExpiryDate                             datetime = NULL,
@UniqueEmail                            int      = 0,
@PasswordFormat                         int      = 0,
@UserId                                 uniqueidentifier OUTPUT

AS BEGIN DECLARE @ApplicationId uniqueidentifier SELECT @ApplicationId = NULL

DECLARE @NewUserId uniqueidentifier
SELECT @NewUserId = NULL

DECLARE @IsLockedOut bit
SET @IsLockedOut = 0

DECLARE @LastLockoutDate  datetime
SET @LastLockoutDate = CONVERT( datetime, '17540101', 112 )

DECLARE @FailedPasswordAttemptCount int
SET @FailedPasswordAttemptCount = 0

DECLARE @FailedPasswordAttemptWindowStart  datetime
SET @FailedPasswordAttemptWindowStart = CONVERT( datetime, '17540101', 112 )

DECLARE @FailedPasswordAnswerAttemptCount int
SET @FailedPasswordAnswerAttemptCount = 0

DECLARE @FailedPasswordAnswerAttemptWindowStart  datetime
SET @FailedPasswordAnswerAttemptWindowStart = CONVERT( datetime, '17540101', 112 )

DECLARE @NewUserCreated bit
DECLARE @ReturnValue   int
SET @ReturnValue = 0

DECLARE @ErrorCode     int
SET @ErrorCode = 0

DECLARE @TranStarted   bit
SET @TranStarted = 0

IF( @@TRANCOUNT = 0 )
BEGIN
    BEGIN TRANSACTION
    SET @TranStarted = 1
END
ELSE
    SET @TranStarted = 0

EXEC dbo.aspnet_Applications_CreateApplication @ApplicationName, @ApplicationId OUTPUT

IF( @@ERROR <> 0 )
BEGIN
    SET @ErrorCode = -1
    GOTO Cleanup
END

SET @CreateDate = DATEADD(hour, +6, @CurrentTimeUtc)
SET @ExpiryDate = DATEADD(day,365, @CreateDate)
SET @DaysLeft = DATEDIFF(day, @CurrentTimeUtc, @ExpiryDate);

SELECT  @NewUserId = UserId FROM dbo.aspnet_Users WHERE LOWER(@UserName) = LoweredUserName AND @ApplicationId = ApplicationId
IF ( @NewUserId IS NULL )
BEGIN
    SET @NewUserId = @UserId
    EXEC @ReturnValue = dbo.aspnet_Users_CreateUser @ApplicationId, @UserName, 0, @CreateDate, @NewUserId OUTPUT
    SET @NewUserCreated = 1
END
ELSE
BEGIN
    SET @NewUserCreated = 0
    IF( @NewUserId <> @UserId AND @UserId IS NOT NULL )
    BEGIN
        SET @ErrorCode = 6
        GOTO Cleanup
    END
END

IF( @@ERROR <> 0 )
BEGIN
    SET @ErrorCode = -1
    GOTO Cleanup
END

IF( @ReturnValue = -1 )
BEGIN
    SET @ErrorCode = 10
    GOTO Cleanup
END

IF ( EXISTS ( SELECT UserId
              FROM   dbo.aspnet_Membership
              WHERE  @NewUserId = UserId ) )
BEGIN
    SET @ErrorCode = 6
    GOTO Cleanup
END

SET @UserId = @NewUserId

IF (@UniqueEmail = 1)
BEGIN
    IF (EXISTS (SELECT *
                FROM  dbo.aspnet_Membership m WITH ( UPDLOCK, HOLDLOCK )
                WHERE ApplicationId = @ApplicationId AND LoweredEmail = LOWER(@Email)))
    BEGIN
        SET @ErrorCode = 7
        GOTO Cleanup
    END
END

IF (@NewUserCreated = 0)
BEGIN
    UPDATE dbo.aspnet_Users
    SET    LastActivityDate = @CreateDate
    WHERE  @UserId = UserId
    IF( @@ERROR <> 0 )
    BEGIN
        SET @ErrorCode = -1
        GOTO Cleanup
    END
END

INSERT INTO dbo.aspnet_Membership
            ( ApplicationId,
              UserId,
              Password,
              PasswordSalt,
              Email,
              LoweredEmail,
              PasswordQuestion,
              PasswordAnswer,
              PasswordFormat,
              IsApproved,
              IsLockedOut,
              CreateDate,
              ExpiryDate,
              DaysLeft,
              LastLoginDate,
              LastPasswordChangedDate,
              LastLockoutDate,
              FailedPasswordAttemptCount,
              FailedPasswordAttemptWindowStart,
              FailedPasswordAnswerAttemptCount,
              FailedPasswordAnswerAttemptWindowStart )
     VALUES ( @ApplicationId,
              @UserId,
              @Password,
              @PasswordSalt,
              @Email,
              LOWER(@Email),
              @PasswordQuestion,
              @PasswordAnswer,
              @PasswordFormat,
              @IsLockedOut,
              @IsLockedOut,
              @CreateDate,
              @ExpiryDate,
              @DaysLeft,
              @CreateDate,
              @CreateDate,
              @LastLockoutDate,
              @FailedPasswordAttemptCount,
              @FailedPasswordAttemptWindowStart,
              @FailedPasswordAnswerAttemptCount,
              @FailedPasswordAnswerAttemptWindowStart )

IF( @@ERROR <> 0 )
BEGIN
    SET @ErrorCode = -1
    GOTO Cleanup
END

IF( @TranStarted = 1 )
BEGIN
    SET @TranStarted = 0
    COMMIT TRANSACTION
END

RETURN 0

清理:

IF( @TranStarted = 1 )
BEGIN
    SET @TranStarted = 0
    ROLLBACK TRANSACTION
END

RETURN @ErrorCode

结尾

4

1 回答 1

2

我希望“今天”列将填充当前时间......在每一行中。

getdate()直接在您的查询中使用并用于datediff()计算日期之间的差异。

select Column1,
       Column2,
       getdate() as Today,
       datediff(day, getdate(), ExpiryDate) as DaysLeft
from YourTable

将当前日期和时间保存在表中是没有意义的,因为它不断变化并且可以使用getdate().

于 2013-01-07T13:07:39.750 回答