[點晴永久免費OA]SQL Server Merge語句
SQL Server MERGE語句簡介假設(shè)有兩個表名為:
下圖說明了 如果單獨使用 但是,SQL Server提供 MERGE target_table USING source_table ON merge_condition WHEN MATCHED THEN update_statement WHEN NOT MATCHED THEN insert_statement WHEN NOT MATCHED BY SOURCE THEN delete; 首先,在 其次, 第三,
SQL Server MERGE語句示例假設(shè)有兩個表: create TABLE sales.category ( category_id INT PRIMARY KEY, category_name VARCHAR(255) NOT NULL, amount DECIMAL(10 , 2 ) ); insert INTO sales.category(category_id, category_name, amount) VALUES(1,'Children Bicycles',15000), (2,'Comfort Bicycles',25000), (3,'Cruisers Bicycles',13000), (4,'Cyclocross Bicycles',10000); create TABLE sales.category_staging ( category_id INT PRIMARY KEY, category_name VARCHAR(255) NOT NULL, amount DECIMAL(10 , 2 ) ); insert INTO sales.category_staging(category_id, category_name, amount) VALUES(1,'Children Bicycles',15000), (3,'Cruisers Bicycles',13000), (4,'Cyclocross Bicycles',20000), (5,'Electric Bikes',10000), (6,'Mountain Bikes',10000); 要使用 MERGE sales.category t USING sales.category_staging s ON (s.category_id = t.category_id) WHEN MATCHED THEN update SET t.category_name = s.category_name, t.amount = s.amount WHEN NOT MATCHED BY TARGET THEN insert (category_id, category_name, amount) VALUES (s.category_id, s.category_name, s.amount) WHEN NOT MATCHED BY SOURCE THEN delete; 執(zhí)行過程如下圖所示 在此示例中,使用兩個表中
在合并的結(jié)果中, 該文章在 2023/7/15 10:22:51 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |