Reputation:
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.
Upvotes: 2
Views: 1480
Reputation: 52808
You cannot pass a function such as getdate() into a stored proc. Declare a datetime variable and use that instead.
Declare @now datetime = getdate();
Execute Sproc_INSERTUPDATE_tblProducts 4, 'Lotus', 'Flower', 85, @now, @now, 0
Upvotes: 2
Reputation: 7430
Try this?
Execute Sproc_INSERTUPDATE_tblProducts 4, 'Lotus', 'Flower', 85, GetDate(), GetDate(), 0
Upvotes: 0