我定義這個SQL Server的SP,但我收到以下錯誤消息,這是不是很詳細:END關鍵字附近的語法錯誤?
Incorrect syntax near the keyword 'end'. 32 8
我關閉所有BEGIN
與END
,所以我不能讓它發動機爲什麼抱怨。
CREATE PROCEDURE dbo.addReading
@deviceId int,
@facilityId int,
@reading real,
@insertionTimestamp datetime2,
@isMeter bit
AS BEGIN
IF (@isMeter = 1)
BEGIN
DECLARE @lastReading real;
DECLARE @newReading real;
-- Get last reading
SELECT @lastReading = lastReading FROM devices
WHERE facilityId = @facilityId AND id = @deviceId;
-- Update lastReading with the new one
UPDATE devices SET lastReading = @reading
WHERE facilityId = @facilityId AND id = @deviceId;
IF (@lastReading IS NOT NULL)
BEGIN
SET @newReading = @reading - @lastReading;
INSERT INTO readings (deviceId, facilityId, reading, insertionTimestamp)
VALUES (@deviceId, @facilityId, @newReading, @insertionTimestamp);
END
ELSE
BEGIN
-- Do nothing
END
END -- ---------------------------------- LINE 32 (ERROR HERE!)
ELSE
BEGIN
INSERT INTO readings (deviceId, facilityId, reading, insertionTimestamp)
VALUES (@deviceId, @facilityId, @reading, @insertionTimestamp);
END
END
GO
END
有什麼問題?
你有一個開始..年底最低一行沒有任何代碼。 Sql服務器不喜歡,刪除它並且錯誤將會消失 – GuidoG
在@lastReading IS NOT NULL後刪除提及的else部分 –