SQL 服务器存储过程如果存在则更新否则插入
SQL Server Stored Procedure IF Exist Update Else Insert
我在本地创建了一个存储过程(IF Exist Update Else Insert)
当我执行程序时它无法插入或更新任何数据,SQL服务器显示查询已成功运行。
表格:
CREATE TABLE Laptops
(
Brand varchar(50),
Series varchar(50),
Model varchar(50),
Gener int,
Ram int,
HDD int,
Processor varchar(50),
Clock float,
Graphic_Chip varchar(50),
Graphic_Memory int,
Qty int,
Price int
PRIMARY KEY (Brand,Series,Model,Gener,Ram,HDD,Processor,Clock,Graphic_Chip,Graphic_Memory)
)
存储过程:
CREATE PROCEDURE Insert_Inventory
(
@Brand Varchar(50),
@Series Varchar(50),
@Model Varchar(50),
@Gener int,
@Ram int,
@HDD int,
@Processor Varchar(50),
@Clock float,
@Graphic_Chip Varchar(50),
@Graphic_Memory int,
@Qty int,
@Price int
)
AS
IF EXISTS (SELECT * FROM Laptops
WHERE Brand = @Brand
and Series = @Series
and Model = @Model
and Gener = @Gener
and ram = @Ram
and hdd = @HDD
and Processor = @Processor
and Clock = @Clock
and Graphic_Chip = @Graphic_Chip
and Graphic_Memory = @Graphic_Memory)
BEGIN
UPDATE Laptops
SET
Qty = Qty + @Qty,
Price = @Price
WHERE
Brand = @Brand
and Series = @Series
and Model = @Model
and Gener = @Gener
and ram = @Ram
and hdd = @HDD
and Processor = @Processor
and Clock = @Clock
and Graphic_Chip = @Graphic_Chip
and Graphic_Memory = @Graphic_Memory
END
ELSE
BEGIN
INSERT into Laptops
VALUES (@Brand, @Series, @Model, @Gener, @Ram, @HDD, @Processor, @Clock, @Graphic_Chip, @Graphic_Memory, @Qty, @Price)
END
执行
exec Insert_Inventory 'Dell', 'Inspiron', '14', 3, 4, 500, 'Core_i_5', 2.7, 'NVIDIA', 512, 20, 42000
您必须将 SP 包装到 BEGIN
和 END
中,您错过了它。并且在 Select Statement
中检查 IF Exist
时也使用 1。并且还要检查,某些事务已打开但未提交。您可以使用 SQL MERGE 来更新和插入语句,而不是也这样做。
CREATE PROCEDURE Insert_Inventory
(
@Brand Varchar(50),
@Series Varchar(50),
@Model Varchar(50),
@Gener int,
@Ram int,
@HDD int,
@Processor Varchar(50),
@Clock float,
@Graphic_Chip Varchar(50),
@Graphic_Memory int,
@Qty int,
@Price int
)
AS
BEGIN
IF EXISTS (SELECT 1 FROM Laptops
WHERE Brand=@Brand
and Series=@Series
and Model=@Model
and Gener=@Gener
and ram=@Ram
and hdd=@HDD
and Processor=@Processor
and Clock=@Clock
and Graphic_Chip=@Graphic_Chip
and Graphic_Memory=@Graphic_Memory
)
BEGIN
UPDATE Laptops set qty=qty+@Qty, Price=@Price
WHERE Brand=@Brand
and Series=@Series
and Model=@Model
and Gener=@Gener
and ram=@Ram
and hdd=@HDD
and Processor=@Processor
and Clock=@Clock
and Graphic_Chip=@Graphic_Chip
and Graphic_Memory=@Graphic_Memory
END
ELSE
BEGIN
INSERT into Laptops Values (@Brand,@Series,@Model,@Gener,@Ram,@HDD,@Processor,@Clock,@Graphic_Chip,@Graphic_Memory,@Qty,@Price)
END
END
我在本地创建了一个存储过程(IF Exist Update Else Insert)
当我执行程序时它无法插入或更新任何数据,SQL服务器显示查询已成功运行。
表格:
CREATE TABLE Laptops
(
Brand varchar(50),
Series varchar(50),
Model varchar(50),
Gener int,
Ram int,
HDD int,
Processor varchar(50),
Clock float,
Graphic_Chip varchar(50),
Graphic_Memory int,
Qty int,
Price int
PRIMARY KEY (Brand,Series,Model,Gener,Ram,HDD,Processor,Clock,Graphic_Chip,Graphic_Memory)
)
存储过程:
CREATE PROCEDURE Insert_Inventory
(
@Brand Varchar(50),
@Series Varchar(50),
@Model Varchar(50),
@Gener int,
@Ram int,
@HDD int,
@Processor Varchar(50),
@Clock float,
@Graphic_Chip Varchar(50),
@Graphic_Memory int,
@Qty int,
@Price int
)
AS
IF EXISTS (SELECT * FROM Laptops
WHERE Brand = @Brand
and Series = @Series
and Model = @Model
and Gener = @Gener
and ram = @Ram
and hdd = @HDD
and Processor = @Processor
and Clock = @Clock
and Graphic_Chip = @Graphic_Chip
and Graphic_Memory = @Graphic_Memory)
BEGIN
UPDATE Laptops
SET
Qty = Qty + @Qty,
Price = @Price
WHERE
Brand = @Brand
and Series = @Series
and Model = @Model
and Gener = @Gener
and ram = @Ram
and hdd = @HDD
and Processor = @Processor
and Clock = @Clock
and Graphic_Chip = @Graphic_Chip
and Graphic_Memory = @Graphic_Memory
END
ELSE
BEGIN
INSERT into Laptops
VALUES (@Brand, @Series, @Model, @Gener, @Ram, @HDD, @Processor, @Clock, @Graphic_Chip, @Graphic_Memory, @Qty, @Price)
END
执行
exec Insert_Inventory 'Dell', 'Inspiron', '14', 3, 4, 500, 'Core_i_5', 2.7, 'NVIDIA', 512, 20, 42000
您必须将 SP 包装到 BEGIN
和 END
中,您错过了它。并且在 Select Statement
中检查 IF Exist
时也使用 1。并且还要检查,某些事务已打开但未提交。您可以使用 SQL MERGE 来更新和插入语句,而不是也这样做。
CREATE PROCEDURE Insert_Inventory
(
@Brand Varchar(50),
@Series Varchar(50),
@Model Varchar(50),
@Gener int,
@Ram int,
@HDD int,
@Processor Varchar(50),
@Clock float,
@Graphic_Chip Varchar(50),
@Graphic_Memory int,
@Qty int,
@Price int
)
AS
BEGIN
IF EXISTS (SELECT 1 FROM Laptops
WHERE Brand=@Brand
and Series=@Series
and Model=@Model
and Gener=@Gener
and ram=@Ram
and hdd=@HDD
and Processor=@Processor
and Clock=@Clock
and Graphic_Chip=@Graphic_Chip
and Graphic_Memory=@Graphic_Memory
)
BEGIN
UPDATE Laptops set qty=qty+@Qty, Price=@Price
WHERE Brand=@Brand
and Series=@Series
and Model=@Model
and Gener=@Gener
and ram=@Ram
and hdd=@HDD
and Processor=@Processor
and Clock=@Clock
and Graphic_Chip=@Graphic_Chip
and Graphic_Memory=@Graphic_Memory
END
ELSE
BEGIN
INSERT into Laptops Values (@Brand,@Series,@Model,@Gener,@Ram,@HDD,@Processor,@Clock,@Graphic_Chip,@Graphic_Memory,@Qty,@Price)
END
END