执行存储过程时"必须声明标量变量"错误

Adi*_*zky 1 sql t-sql sql-server stored-procedures sql-server-2012

我试图制作一个插入价格的程序:

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
Run Code Online (Sandbox Code Playgroud)

当我执行程序时没关系,但是当我运行这样的程序时:

EXEC prInsertPrice 'N01', 20000, @date, null
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

必须声明标量变量@date.

为什么这样,我该如何纠正这个问题?

小智 5

exec语句中的@date与存储过程中的@date不同.

你应该做的事情如下:

DECLARE @date AS DATETIME
SET @date = GETDATE()
EXEC prInsertPrice 'N01', 20000, @date, null
Run Code Online (Sandbox Code Playgroud)

  • 他确实一步一步引导你.事实上,它为您提供了所需的所有代码.它的哪一部分不清楚? (5认同)
  • 还是不明白,能不能一步一步指导我?,我是菜鸟 (2认同)