Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a REPLACE INTO query good practice?

I needed an SQL query that would update or create a database entry (if a certain customer doesn't 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 it used a lot and actually never heard of it before, is this really the solution I want, or should I do this manually?

like image 328
Bart Burg Avatar asked Jul 11 '12 08:07

Bart Burg


1 Answers

Both REPLACE INTO and the ON DUPLICATE KEY alternative suggested are non standard SQL from the MySQL variant. So whether you use it depends on a) whether you're using MySQl and b) whether you want to be tied to that variant.

ANSI SQL defines a MERGE syntax that is more standard, if it is implemented on your platform

like image 59
podiluska Avatar answered Oct 04 '22 01:10

podiluska