我试图制作一个插入价格的程序:
create procedure prInsertPrice @NuggetID varchar(5), @Unit_Price money, @Start_Date datetime, @End_Date datetime as begin DECLARE @date AS DATETIME SET @date = GETDATE() if ( (@NuggetID like 'N[0-9][0-9]') and (@Unit_Price is not null) and (@Start_Date is not null) ) begin print 'Insert Success' insert NuggetPrice (NuggetId, Unit_Price, Start_Date, End_Date) values (@NuggetID, @Unit_Price, @Start_Date, @End_Date) end else begin print 'Failed to insert' end end
当我执行程序时没关系,但是当我运行这样的程序时:
EXEC prInsertPrice 'N01', 20000, @date, null
我收到错误消息:
必须声明标量变量@date.
为什么这样,我该如何纠正这个问题?
exec语句中的@date与存储过程中的@date不同.
你应该做的事情如下:
DECLARE @date AS DATETIME SET @date = GETDATE() EXEC prInsertPrice 'N01', 20000, @date, null