目前我正在尝试在SQL Server中创建存储过程。
这是我的存储过程:
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
关键字“IF”附近的语法不正确。Msg 156,Level 15,State 1,Server e8535208dabe,Procedure sp_MasterManagement,Line 27
关键字“IF”附近的语法不正确。Msg 156,Level 15,State 1,Server e8535208dabe,Procedure sp_MasterManagement,Line 30
关键字“END”附近的语法不正确。
我不知道,我不明白为什么我会收到此错误。到目前为止,我相信我的语法很好。有什么想法吗?
答案 0 :(得分:0)
你走了:
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 Scope_Identity);
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