我正在尝试创建一个存储过程,它将接受一个表值参数,然后使用merge来添加新记录,保留已存在的那些并删除已存在但尚未提供的记录,这就是我想要的,
Alter procedure [dbo].[InsertMultipleFruits]
(
@FruitCrates As Fruits_CratesType READONLY
)
AS
GO
DECLARE @I INT;
BEGIN
MERGE INTO Fruits_Crates AS TARGET
USING(SELECT FruitID, CrateID FROM @FruitCrates) AS SOURCE
ON (TARGET.FruitID = SOURCE.FruitID)
WHEN MATCHED THEN
UPDATE
SET @I = 1 --- i want nothing to happen here ---
WHEN NOT MATCHED BY TARGET THEN
INSERT (FruitID, CrateID)
VALUES (SOURCE.FruitID, SOURCE.CrateID)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
END
我收到错误,
...第5行必须声明表变量" @ FruitCrates"。 信息156,第15级,状态1,第10行关键字' WHEN'附近的语法不正确。
我已经创建了表类型,但不确定为什么会出错。
答案 0 :(得分:2)
不要在程序声明和正文之间放置go
。试试这个:
Alter procedure [dbo].[InsertMultipleFruits]
(
@FruitCrates As Fruits_CratesType READONLY
)
AS
BEGIN
DECLARE @I INT;
MERGE INTO Fruits_Crates AS TARGET
USING(SELECT FruitID, CrateID FROM @FruitCrates) AS SOURCE
ON (TARGET.FruitID = SOURCE.FruitID)
WHEN MATCHED THEN
UPDATE
SET @I = 1 --- i want nothing to happen here ---
WHEN NOT MATCHED BY TARGET THEN
INSERT (FruitID, CrateID)
VALUES (SOURCE.FruitID, SOURCE.CrateID)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
END;