[点晴永久免费OA]SQL Server Merge语句
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
SQL Server MERGE语句简介假设有两个表名为:
下图说明了 如果单独使用 但是,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语句示例假设有两个表: 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; 执行过程如下图所示 在此示例中,使用两个表中
在合并的结果中, 该文章在 2023/7/15 10:22:51 编辑过 |
关键字查询
相关文章
正在查询... |