2

When you need to change data in a MySQL table we used this command: Replace into table_name

But for Sql Server I have no idea how to use a similar command.

Does anyone could help me please.

I liked to replace this:

("replace into products"
   + " (date, hour, IdProduct, Ref1, Ref2, Ref3) values (?,?,?,?,?,?)");

I thank you all for your help. And I'm sorry.

greetings

1
  • 1
    Check out the MERGE statement Commented May 26, 2014 at 11:43

4 Answers 4

2

Without REPLACE INTO or INSERT ON DUPLICATE KEY UPDATE, you need to find out which records should be updated or inserted. I had to do something like this in cases where there was no composite primary key, but it would work as a substitute for REPLACE INTO.

  • Create a temporary table that has the columns to be INSERTED or UPDATED. Also in this temp table you need a "found" column of type BIT
  • Put the rows that are to be inserted or updated into this temp table. Leave "found" as 0.
  • Do UPDATE temporary table SET found = 1 and join to your destination table.
  • Update your destination table JOINed to the temporary table on the primary key (or unique key) fields.
  • INSERT into the destination table everything from the temporary table where found = 0.

SQL coming as soon as the page will let me post it, having technical issues here. For now, added a picture.

enter image description here

EDIT: The SQL in the picture, in TEXT format:

DROP TABLE IF EXISTS
CREATE TABLE #tmp (
 col_1, col_2, col_n
   found bit NOT NULL DEFAULT 0,
   PRIMARY KEY CLUSTERED (pkey_column1, pkey_column2, pkey_column_n)
);
INSERT INTO #tmp (col_1, col_2, col_n)
SELECT
col_1, col_2, col_n
FROM source_table;

UPDATE a
SET
a.found = 1
FROM #tmp a
INNER JOIN destination_table b
ON a.pkey_column_1 = b.pkey_column_1
AND a.pkey_column_2 = b.pkey_column_2
AND a.pkey_column_n = b.pkey_column_n;

UPDATE b
SET
b.col_1 = a.col_1,
b.col_2 = a.col_2,
b.col_n = a.col_n
FROM #tmp a
INNER COIN destination_table_b
ON a.pkey_column_1 =  b.pkey_column_1
AND a.pkey_column_2 = b.pkey_column_2
AND a.pkey_column_n = b.pkey_column_n;

INSERT INTO destination_table
(col_1, col2, col_n)
SELECT
col1_, col_2, col_n
FROM #tmp
WHERE found = 0

Sign up to request clarification or add additional context in comments.

Comments

1
UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;

1 Comment

Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
0
MERGE INTO target_table AS target
USING source_table AS source
ON target.key = source.key
WHEN MATCHED THEN
    UPDATE SET target.col1 = source.col1
WHEN NOT MATCHED BY TARGET THEN
    INSERT (col1, col2) VALUES (source.col1, source.col2)
WHEN NOT MATCHED BY SOURCE THEN
    DELETE;

Merge is the right sentence that could do same as REPLACE INTO on mysql

Comments

-3

In SQL Server you can use UPDATE Command for changing data in a table:

UPDATE products
  SET date = new_value_1,
      hour = new_value_2,
      ...
      ...
WHERE (where_clause)

2 Comments

Update is not even part of the mysql REPLACE INTO clause. It's a DELETE and INSERT if exists, else INSERT. (I personally prefer UPDATE and INSERT you get with MERGE tho)
Downvoted because the question is in regards to REPLACE INTO. UPDATE will work but doesn't fulfill the same need as REPLACE since it requires you to already know which records need to be updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.