현재 SQL Server에서 저장 프로 시저를 만들려고합니다.잘못된 구문, if 절로 인해 저장 프로 시저를 만들 수 없습니까?
이 내 저장 프로 시저입니다 : 스크립트를 실행하는 순간에
CREATE PROCEDURE sp_MasterManagement
@ProductId INT,
@ProductName NVARCHAR(40),
@SupplierID INT,
@CategoryID INT,
@QuantityPerUnit NVARCHAR(20),
@UnitPrice DECIMAL(10, 5),
@UnitsInStock SMALLINT,
@UnitsOnOrder SMALLINT,
@ReorderLevel SMALLINT,
@Discontinued BIT,
@StatementType VARCHAR
AS
BEGIN
IF @StatementType = 'INSERT'
BEGIN
INSERT INTO [dbo].Products (ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued)
VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued);
DECLARE @LastInserted INT;
SELECT @LastInserted = SELECT ScopeIdentity();
SELECT *
FROM [dbo].Products
WHERE ProductID = @LastInserted
END
IF @StatementType = 'UPDATE'
BEGIN
UPDATE Products
SET ProductName = @ProductName,
SupplierID = @SupplierID,
CategoryID = @CategoryID,
QuantityPerUnit = @QuantityPerUnit,
UnitPrice = @UnitPrice,
UnitsInStock = @UnitsInStock,
UnitsOnOrder = @UnitsOnOrder,
ReorderLevel = @ReorderLevel,
Discontinued = @Discontinued
WHERE ProductID = @ProductID;
SELECT *
FROM Products
WHERE ProductID = @ProductID
END
END
GO
그러나,이 오류가 얻을 :
Msg 156, Level 15, State 1, Server e8535208dabe, Procedure sp_MasterManagement, Line 24
Incorrect syntax near the keyword 'IF'.Msg 156, Level 15, State 1, Server e8535208dabe, Procedure sp_MasterManagement, Line 27
Incorrect syntax near the keyword 'IF'.Msg 156, Level 15, State 1, Server e8535208dabe, Procedure sp_MasterManagement, Line 30
Incorrect syntax near the keyword 'END'.
모르겠다을 나는 이해가 안 돼요 왜이 오류가 발생하는지. 지금까지는 구문이 잘 형성되어 있다고 생각합니다. 어떤 아이디어? 여기
을 시도해보십시오이'(SELECT SCOPE_IDENTITY())'대신'ScopeIdentity()' – Sami
사이드 노트를 선택 : 당신은 **하지 **의'sp_' 접두어를 사용한다 저장 프로 시저. Microsoft는 [자체 저장을 위해이 접두어를 예약했습니다 (* 저장 프로 시저 명명 * 참조)] (http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx) 및 당신은 미래에 언젠가 이름 충돌의 위험을 감수해야합니다. [저장 프로 시저 성능에 좋지 않습니다.] (http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix). 'sp_'를 피하고 다른 것을 접두어로 사용하는 것이 가장 좋습니다. –