gpt4 book ai didi

sql-server - 是否可以在旨在插入数据的存储过程中插入当前日期时间?

转载 作者:行者123 更新时间:2023-12-01 21:33:23 25 4
gpt4 key购买 nike

我创建了以下存储过程:

CREATE PROCEDURE dbo.InsertTutees 
@StudentID INT = NULL,
@FirstName VARCHAR(50) = NULL,
@Surname VARCHAR(50) = NULL,
@Major VARCHAR(50) = NULL,
@Timestamp DATETIME = NULL
AS
BEGIN
SET NOCOUNT ON

INSERT INTO dbo.Tutees (StudentID, FirstName, Surname, Major, Timestamp)
VALUES (@StudentID, @FirstName, @Surname, @Major, @Timestamp)
END
GO

EXEC dbo.InsertTutees
@StudentID = 2,
@FirstName = 'Sarah',
@Surname = 'Smith',
@Major = 'Science',
@Timestamp = '2013-12-12';

但是我想用当前时间戳替换时间戳值。我试过 GETDATE(),但出现以下错误:

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'

是否有其他方法可以在此 exec 语句中检索当前日期?我应该编辑我的存储过程吗?

感谢您的帮助!

编辑:这是我尝试完成的 exec 语句:

EXEC dbo.InsertTutees
@StudentID = 2,
@FirstName = 'Sarah' ,
@Surname = 'Smith' ,
@Major = 'Science' ,
@Timestamp = getdate()

最佳答案

你可以使用一个中间变量:

    DECLARE @dt datetime =GETDATE()

EXEC dbo.InsertTutees
@StudentID = 2,
@FirstName = 'Sarah' ,
@Surname = 'Smith' ,
@Major = 'Science' ,
@Timestamp = @dt

关于sql-server - 是否可以在旨在插入数据的存储过程中插入当前日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62302838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com