在ASPNETDB.MDF中创建“今天”列时,getdate()不会是常量

时间:2013-01-07 13:01:15

标签: asp.net database sql-server-2005

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

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

N:B:我是一个初学者,我认为这里有各种各样的解决方案,我已经看过,但是会让我感到困惑,哪一个会解决我的问题。所以这个问题很无聊,我很抱歉。

    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 开始     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

END

1 个答案:

答案 0 :(得分:2)

  

我想'今天'专栏将充满现在的时间......在每一个   行。

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

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

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