将内置函数传递给存储过程参数

时间:2013-05-12 13:13:50

标签: sql stored-procedures sql-server-2008-r2

我有一个存储过程(SP1),我希望它调用另一个存储过程(SP2)。

代码SP2

ALTER PROCEDURE dbo.addOrderStatus
    (
    @orderID INT,
    @statusID INT,
    @startTime DATETIME,
    @endTime DATETIME,
    @isActive BIT
    )
AS
BEGIN
    SET NOCOUNT ON
    INSERT INTO Order_Status
    (orderID, statusID, startTime, endTime, isActive) VALUES
    (@orderID, @statusID, @startTime, @endTime, @isActive)
END

代码SP1

ALTER PROCEDURE [dbo].addNewOrder
(
    @customerID int,
    @restaurantID int,
    @cost float,
    @addressID int,
    @ID INT OUTPUT
)
AS
    SET NOCOUNT OFF;

INSERT INTO [Order] 
([customerID], [restaurantID], [cost], [addressID]) 
VALUES (@customerID, @restaurantID, @cost, @addressID);

set @ID = @@IDENTITY

EXEC addOrderStatus @ID, @statusID = 1, @startTime =  SYSDATETIME , @endTime = NULL, @isActive = TRUE

我的问题

我得到了这个例外

Error converting data type nvarchar to datetime.

我怎么解决?

感谢提前

在您的回答和评论之后

代码是

DECLARE @startTime DATETIME
SET @startTime = SYSDATETIME
EXEC addOrderStatus @ID, @statusID = 1, @startTime , @endTime = NULL, @isActive = TRUE

现在我知道我必须使用此表单name , value

2 个答案:

答案 0 :(得分:0)

编辑 - 错误的部分已删除

首先声明你的变量,设置内容然后传递它们。

DECLARE @starttime datetime;
DECLARE @statusID int;
DECLARE @endTime datetime;
DECLARE @isActive int;

SET @startTime =  GETDATE();
SET @statusID = 1;
SET @endTime = null;
SET @isActive = 1;

EXEC addOrderStatus @ID, @statusID, @startTime , @endTime, @isActive

答案 1 :(得分:0)

而不是:

EXEC addOrderStatus @ID, @statusID = 1, @startTime =  SYSDATETIME , @endTime = NULL, @isActive = TRUE

试试这个:

declare @sDate datetime;
set @sDate=getdate();
EXEC addOrderStatus @ID, @statusID = 1, @startTime =  @sDate , @endTime = NULL, @isActive = TRUE