我正在尝试开发一个T-SQL存储过程并在那里执行一些逻辑和计算,但由于某种原因导致语法错误。我将突出关键的地方,我不知道该怎么做。
CREATE PROC AddInvestment
@_Investor nvarchar(89),
@_Sum decimal(19,4),
@_Currency smallint,
@_Rate tinyint,
@_Date date,
@_Comment nvarchar(max)
AS
DECLARE @_MavroRate decimal(19,4)
DECLARE @_Mavro decimal(19,4)
SELECT mr.[40] INTO @_MavroRate <==== ERROR
FROM dbo.MavroRateBuy mr
WHERE mr.Date = @_Date
@_Mavro = _Sum / @_MavroRate <==== ERROR
INSERT INTO Investments(Investor, Sum, Currency, Rate, Date, Mavro, MavroRate, Comment)
VALUES (@_Investor, @_Sum, @_Currency, @_Rate, @_Date, @_Mavro, @_MavroRate, @_Comment)
请你通过T-SQL声明验证并指出我在这里做错了什么。
谢谢!
答案 0 :(得分:2)
我想这样的事情:
SELECT @_MavroRate = mr.[40]
FROM dbo.MavroRateBuy mr
WHERE mr.Date = @_Date
SELECT @_Mavro = @_Sum / @_MavroRate
@_ Sum在使用之前需要有一个值。
答案 1 :(得分:1)
试试这个:
CREATE PROC AddInvestment
@_Investor nvarchar(89),
@_Sum decimal(19,4),
@_Currency smallint,
@_Rate tinyint,
@_Date date,
@_Comment nvarchar(max)
AS
DECLARE @_MavroRate decimal(19,4)
DECLARE @_Mavro decimal(19,4)
SELECT @_MavroRate = mr.[40]
FROM dbo.MavroRateBuy mr
WHERE mr.Date = @_Date
SET @_Mavro = @_Sum / @_MavroRate
INSERT INTO Investments(Investor, Sum, Currency, Rate, Date, Mavro, MavroRate, Comment)
VALUES (@_Investor, @_Sum, @_Currency, @_Rate, @_Date, @_Mavro, @_MavroRate, @_Comment)
答案 2 :(得分:1)
您错过了SET
行前面的@_Mavro = _Sum / @_MavroRate
。它应该看起来像下面的代码。
SET @_Mavro = _Sum / @_MavroRate
此外,INTO
用于将结果插入新表中,而不是设置变量。下面的代码是在select语句中设置变量的正确方法。
SELECT @_MavroRate = mr.[40]
FROM dbo.MavroRateBuy mr
WHERE mr.Date = @_Date