Reputation: 3
Site is already built in .Net where it is using SQL server stored procedure. Now we are converting into PHP for that we are also altering stored procedure in MySQL.
In one of stored procedure there is some code to multiple insert and update which is achieved in SQL stored procedure as follows:
MERGE
INTO products T
USING SELECT STATEMENT
WHEN MATCHED
THEN UPDATE STATEMENT
WHEN NOT MATCHED
THEN INSERT STATEMENT
I need an alternative to this approach in MySQL stored procedure syntax. Please guide me through how can I achieve this.
Upvotes: 0
Views: 72
Reputation: 1270061
You should be able to replace this with on duplicate key update
.
For this, you will need a unique key on the columns that specify "matched".
Note: This will (definitely) not replace all merge
code. However, your example seems like it is implementing exactly this logic. Otherwise, you can use if
with update
and insert
statements to pretty much do the same thing.
Upvotes: 1