sql 示例:如果行存在,则如何更新行或如果行不存在则插入行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 示例:如果行存在,则如何更新行或如果行不存在则插入行相关的知识,希望对你有一定的参考价值。
-- three methods for UPDATing if a row exists, ELSE INSERTing if the row doesn't exists. There are times where you want to overwrite a row if the row already exists, or insert a row if the row doesn't exists. For example, if a customer_no already exists, update the last_order column, and if not, insert a row for the customer_no and then insert and new last_order.
-- first method, using set based MERGE INTO, which WHEN MATCHED will update the row, and WHEN NOT MATCHED will add the row. You have a target table that will be updated, and then a staging table that contains the new info that you want updated or added to the target table. It matches the rows based on the cn_fy column found in both tables, and when it finds a match, it executes the UPDATE statement, and when it doesn't find a match, it executes the INSERT statement.
MERGE INTO forecast f
USING forecast2 s
ON f.cn_fy = s.cn_fy
WHEN MATCHED THEN
UPDATE SET f.name = s.name2, f.amount = s.amount
WHEN NOT MATCHED THEN
INSERT (customer_no, name, fiscalyear, amount)
VALUES (s.customer_no, s.name2, s.fiscalyear, s.amount);
-- second method that checks if a row has a customer_no of 2255 AND a fiscalyear=2018, using IF EXISTS THEN, ELSE. This checks IF the SELECT statement returns a value, and if so, it executes the UPDATE statement. If it doesn't return a value, then it executes the INSERT statement.
DECLARE @cn INT = 2255;
DECLARE @fy INT = 2018;
IF EXISTS (SELECT ref_no FROM forecast WHERE customer_no=@cn AND fiscalyear=@fy)
UPDATE forecast SET Amount=700 WHERE customer_no=@cn AND fiscalyear=@fy
ELSE
INSERT INTO forecast (customer_no,fiscalyear,name,amount)
VALUES
(2255,@fy,'ben smoth',200000);
------------------------------
--DETAIL OF IF EXISTS METHOD--
------------------------------
DECLARE @cn INT = 2255;
DECLARE @fy INT = 2018;
-- IF the SELECT statement in the quotes returns a value (if the value exists), then that means the row already exists and needs to be updated
IF EXISTS (SELECT ref_no FROM forecast WHERE customer_no=@cn AND fiscalyear=@fy)
--update the row if it exists
UPDATE forecast SET Amount=700 WHERE customer_no=@cn AND fiscalyear=@fy
-- ELSE if the SELECT statement doesn't return a value (it doesn't exist), that means the row needs to be added. In this case, execute the ELSE statement which is an INSERT.
ELSE
INSERT INTO forecast (customer_no,fiscalyear,name,amount)
VALUES
(2255,@fy,'ben smoth',200000);
-- third method using IF @@ROWCOUNT=0, which tries update and if @@ROWCOUNT is 0, it then executes the INSERT statement. There might be some performance benefits to this method over the IF EXISTS(SELECT) method.
DECLARE @cn INT = 2255;
DECLARE @fy INT = 2018;
UPDATE forecast SET Amount=73474 WHERE customer_no=@cn AND fiscalyear=@fy
IF @@ROWCOUNT=0
INSERT INTO forecast (customer_no,fiscalyear,name,amount)
VALUES
(2255,@fy,'ben smoth',2357340000);
-------------------------------
--DETAIL OF @@ROWCOUNT METHOD--
-------------------------------
DECLARE @cn INT = 2255;
DECLARE @fy INT = 2018;
-- go straight to updating where the conditions are met
UPDATE forecast SET Amount=73474 WHERE customer_no=@cn AND fiscalyear=@fy
-- if the conditions where found then that row will have been updated, and @@ROWCOUNT will be 1, therefore the IF statement below won't do anything. However, if the conditions weren't found and therefore a row wasn't updated, then @@ROWCOUNT will be 0, in which case the IF statement below will execute the INSERT statement to create the row.
IF @@ROWCOUNT=0
INSERT INTO forecast (customer_no,fiscalyear,name,amount)
VALUES
(2255,@fy,'ben smoth',2357340000);
以上是关于sql 示例:如果行存在,则如何更新行或如果行不存在则插入行的主要内容,如果未能解决你的问题,请参考以下文章
如果行不是重复的,或者行的 ID 不是 SQL SSMS 中的某个数字,则更新列