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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With