Bart Burg
Bart Burg

Reputation: 4924

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?

Upvotes: 7

Views: 4834

Answers (2)

podiluska
podiluska

Reputation: 51504

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

Upvotes: 5

juergen d
juergen d

Reputation: 204854

it is more common to use

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

replace into actually deletes a duplicate entry and inserts a new one.

Upvotes: 5

Related Questions