


How Does SQL Server's MERGE Statement Mimic MySQL's ON DUPLICATE KEY UPDATE?
SQL Server's Counterpart to MySQL's ON DUPLICATE KEY UPDATE
In MySQL, the ON DUPLICATE KEY UPDATE clause allows users to update existing rows instead of inserting new ones if a duplicate key is encountered. This feature is particularly useful when dealing with unique constraints and primary keys.
SQL Server's MERGE Statement
While SQL Server does not have an exact equivalent to MySQL's ON DUPLICATE KEY UPDATE, it does provide the MERGE statement as a comparable solution. MERGE combines the functionality of INSERT, UPDATE, and DELETE statements into a single query, allowing for efficient handling of duplicate values.
Implementing ON DUPLICATE KEY UPDATE Using MERGE
To implement the on-duplicate-update behavior in SQL Server, you can use the following syntax:
MERGE INTO [target_table] AS target USING [source_table] AS source ON (target.[matching_column] = source.[matching_column]) WHEN MATCHED THEN UPDATE SET [target_column] = [source_column] WHEN NOT MATCHED THEN INSERT ([target_column]) VALUES ([source_column]);
Example
Consider the following query that inserts data into a table named METER_DATA and updates row values if a duplicate exists based on the rtu_id and time_local columns:
MERGE INTO MyBigDB.dbo.METER_DATA WITH (HOLDLOCK) AS target USING ( SELECT 77748 AS rtu_id, '12B096876' AS meter_id, 56112 AS meter_reading, '20150602 00:20:11' AS time_local ) AS source (rtu_id, meter_id, meter_reading, time_local) ON (target.rtu_id = source.rtu_id AND target.time_local = source.time_local) WHEN MATCHED THEN UPDATE SET meter_id = '12B096876', meter_reading = 56112 WHEN NOT MATCHED THEN INSERT (rtu_id, meter_id, meter_reading, time_local) VALUES (77748, '12B096876', 56112, '20150602 00:20:11');
This query will insert the values from the source table into the target table if a row with the same rtu_id and time_local does not already exist. If a duplicate row does exist, the update will be performed instead, preserving the existing data in the target table.
The above is the detailed content of How Does SQL Server's MERGE Statement Mimic MySQL's ON DUPLICATE KEY UPDATE?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.
