Is REPLACE INTO a good practice?

I need a SQL query that will update or create a record in the database (if some client does not exist yet). I found the current solution on the Internet:

command.CommandText = "REPLACE INTO [Resource_Tracer].[dbo].[Customer](CustomerName, CustomerID) VALUES (@CustomerName, @CustomerID)" 

Since I don’t see him use much and never really hear about it before, is this really the solution I want, or should I do it manually?

+6
source share
2 answers

Both proposed alternatives to REPLACE INTO and ON DUPLICATE KEY are non-standard SQL from the MySQL variant. Thus, regardless of whether you use it, it depends: a) whether you use MySQl and b) whether you want to be attached to this option.

ANSI SQL defines MERGE syntax, which is more standard if implemented on your platform

+5
source

more commonly used

 INSERT INTO table col1) VALUES (1) ON DUPLICATE KEY UPDATE col1=VALUES(Col1) 

replace into actually removes the duplicate entry and inserts a new one.

+2
source

Source: https://habr.com/ru/post/920191/


All Articles