插入/更新存储过程有转换错误
我正在尝试仅在 SQL Server 中创建一个存储过程,让用户选择是否要添加或更新记录。以下是我的存储专业版的代码:
CREATE PROCEDURE Sproc_INSERTUPDATE_tblProducts
@ProductID bigint,
@ProductName varchar(50),
@Description varchar(50),
@Price money,
@DateCreated datetime,
@DateUpdated datetime,
@Choice bit output
AS
BEGIN
Select @Choice
If @Choice = 0
Begin
Insert into tblProducts (
ProductID,
ProductName,
Description,
Price,
DateCreated,
DateUpdated)
values (@ProductID,
@ProductName,
@Description,
@Price,
@DateCreated,
@DateUpdated)
Select * from tblProducts
End
Else If @Choice = 1
Begin
Update tblProducts Set ProductID = @ProductID,
ProductName = @ProductName,
Description = @Description,
Price = @Price,
DateCreated = @DateCreated,
DateUpdated = @DateUpdated
Select * from tblProducts
End
Else
Begin
Print 'Invalid choice. Please choose 0 or 1 only.'
End
END
GO
这是我用于执行我制作的存储专业版的代码:
USE StoreDB
Execute Sproc_INSERTUPDATE_tblProducts 4, 'Lotus', 'Flower', 85, GetDate, GetDate, 0
我的存储专业版没有遇到任何错误,但是当我尝试使用新查询执行时,我收到以下错误消息:
GetDate():将数据类型 nvarchar 转换为日期时间时出错。
I am trying to make one stored procedure only in SQL Server that lets the user to choose if he/she wants to add or update a record. Below is my code for my stored pro:
CREATE PROCEDURE Sproc_INSERTUPDATE_tblProducts
@ProductID bigint,
@ProductName varchar(50),
@Description varchar(50),
@Price money,
@DateCreated datetime,
@DateUpdated datetime,
@Choice bit output
AS
BEGIN
Select @Choice
If @Choice = 0
Begin
Insert into tblProducts (
ProductID,
ProductName,
Description,
Price,
DateCreated,
DateUpdated)
values (@ProductID,
@ProductName,
@Description,
@Price,
@DateCreated,
@DateUpdated)
Select * from tblProducts
End
Else If @Choice = 1
Begin
Update tblProducts Set ProductID = @ProductID,
ProductName = @ProductName,
Description = @Description,
Price = @Price,
DateCreated = @DateCreated,
DateUpdated = @DateUpdated
Select * from tblProducts
End
Else
Begin
Print 'Invalid choice. Please choose 0 or 1 only.'
End
END
GO
And here is my code for executing the stored pro I made:
USE StoreDB
Execute Sproc_INSERTUPDATE_tblProducts 4, 'Lotus', 'Flower', 85, GetDate, GetDate, 0
I don't encounter any errors with my stored pro but when I try to execute using a new query, I get this error message:
GetDate(): Error converting data type nvarchar to datetime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将诸如 getdate() 之类的函数传递到存储过程中。声明一个日期时间变量并使用它。
You cannot pass a function such as getdate() into a stored proc. Declare a datetime variable and use that instead.
试试这个?
Try this?