是否可以在旨在插入数据的存储过程中插入当前日期时间?
Is it possible to insert current datetime in a stored procedure which is designed to insert data?
我创建了以下存储过程:
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
如果您查找 in the documentation,默认值必须是常量或 NULL。为了获得等于 GETDATE() 的默认值,您需要在 proc 中按照以下几行编写一些代码:
IF @Timestamp IS NULL
BEGIN
SET @Timestamp = GETDATE();
END
这将允许您根据需要提供值和默认值。
我创建了以下存储过程:
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
如果您查找 in the documentation,默认值必须是常量或 NULL。为了获得等于 GETDATE() 的默认值,您需要在 proc 中按照以下几行编写一些代码:
IF @Timestamp IS NULL
BEGIN
SET @Timestamp = GETDATE();
END
这将允许您根据需要提供值和默认值。