sql - "Must declare the scalar variable"执行存储过程时出错

标签 sql sql-server tsql 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

当我执行程序时很好,但是当我像这样运行程序时:

EXEC prInsertPrice 'N01', 20000, @date, null

我收到错误消息:

Must declare the scalar variable @date.

为什么会这样,我该如何解决这个问题?

最佳答案

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

你应该这样做:

DECLARE @date AS DATETIME
SET @date = GETDATE()
EXEC prInsertPrice 'N01', 20000, @date, null

关于sql - "Must declare the scalar variable"执行存储过程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594616/

相关文章:

sql - SQL 连接的补充?需要 T-SQL 帮助

sql - 每个日期时间增量一秒的填充表

sql - ORDER BY 语句中的方程

Java SQL 查询执行,读取 ResultSet 行时出现问题

sql - 无法使用 "Select TOP @Count ..."

sql - NOLOCK与事务隔离级别

sql - 使用条件设置行安全性

python - Mssql外键

围绕间隙的 SQL 分组

sql - 合并来自 T-SQL 的 XML