Questions tagged [sql-merge]

`SQL MERGE` statement allows us to insert new rows into table and update existing rows depending on given condition. Use this tag in addition to [tag:sql] to make question better categorized.

SQL MERGE statement allows us to insert new rows into table and update existing rows depending on given condition.

Examples of using MERGE statement:

Use this tag in addition to to make question better categorized.

324 questions
3
votes
2 answers

basic MERGE into same table

What I am trying to do is insert/update different versions into a product table. This is a PL/SQL chunk and i have a variable that holds the version number. Lets say the version is 10. Now, I wanna use the merge statement to insert or update…
Curtis
  • 55
  • 1
  • 2
  • 5
2
votes
1 answer

Merging tables on specific fields in PrestoDB

I want to combine two different tables. table1 name id type date name1 1 a 08-17 name2 2 b 08-17 table2 name id date attribute_1 name1 1 08-18 attr_id_1 name2 2 08-18 attr_id_2 The two tables have in common "name" and…
dhlbryan
  • 21
  • 3
2
votes
2 answers

What is output in case of concurrent transactions in oracle

Suppose, i issued merge statement to merge two tables. There is a huge amount of data, so merge command was running. In midway, a different user inserted new data in source table. So, will the new data be read as a part of merge command?
Tulika
  • 128
  • 8
2
votes
1 answer

SQL Server Merge with two tables from different azure SQL Database from same Azure SQL SERVER

I need to merge with 2 different tables from 2 different azure SQL databases where as these two azure sql database are from same azure sql server. also for performance imporvement purpose, what I need to do is bulk insert and/or bulk update. also,…
2
votes
1 answer

PL/SQL developer export query result as merge statements

I just started to use PL/SQL Developers tools. Before I used to use Toad for Oracle. In Toad, I can generate Merge Statement from the Query result for every records and it give me following example result. (for single result) MERGE user T USING ( …
gourcam
  • 21
  • 4
2
votes
2 answers

SQL MERGE only when changed

On a SQL MERGE how do I insert a "do nothing" type statement? For example, I have this right now: MERGE INTO dbo.Worker AS T USING dbo.Collector_Worker AS S ON T.WWID = S.WWID WHEN MATCHED THEN UPDATE SET T.IDSID = S.IDSID, ... WHEN NOT…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
2
votes
2 answers

How to insert multiple rows in a merge?

How to insert multiple rows in a merge in SQL? I'm using a MERGE INSERT and I'm wondering is it possible to add two rows at the same time? Under is the query I have written, but as you can see, I want to insert both boolean for IsNew, also when it…
Ken Kaneki
  • 57
  • 1
  • 9
2
votes
1 answer

SQL Server - coalesce data on duplicate keys when MERGE-ing

I've stumbled across an annoying situation where my source query results have duplicate keys with differing data. Unfortunately I need to back-fill any NULLs. I tried with a MERGE but I get a key error. The equivalent query in MySQL (that I cannot…
Stefan Collier
  • 4,314
  • 2
  • 23
  • 33
2
votes
1 answer

Merge, delete when not on Source

I have an issue with a merge statement into a FACT TABLE It was pretty simple until the users started to delete records from the source. Current SQL: Set Count = 1 WHEN NOT MATCHED INSERT WHEN MATCHED UPDATED New SQL: So in this example, a…
StackUser1000
  • 23
  • 1
  • 3
2
votes
0 answers

Which approach is better for UPSERT in PostgreSQL?

After reading at How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL? I'm curious which approach is better (and faster) for UPSERT. I am currently writing an application where in most cases data only updates (new rows are rare)…
2
votes
1 answer

Oracle 11g Merge ORA-00905: missing keyword

I can't seem to find what's missing in my query : MERGE INTO Account D USING (SELECT 1 AS ID ,'AAAA' AS NAME ,'123Z4' AS GUID , 1 AS STATUS FROM DUAL ) S ON…
Jay
  • 21
  • 2
2
votes
1 answer

AS400 - Using Merge to add a new record to a table and prevent from a duplicate key error

I have created a new table VISNHDR with the following fields (it is not the entire table but this is enough for my question): - ORDNO (VARCHAR 7) - KEY - ORDDT (NUMERIC 7,0) - KEY - ORDTM (NUMERIC 6,0) - KEY - CRTDT (NUMERIC 7,0) -…
ehh
  • 3,412
  • 7
  • 43
  • 91
2
votes
2 answers

Oracle sql MERGE INTO with a single where clause

I have the following SQL code (this is how much I've got so far): MERGE INTO SCHEMA1.TABLE_1 table1 USING ( SELECT DISTINCT table2.column1, view1.column2 FROM SCHEMA2.TABLE_2 table2 LEFT JOIN SCHEMA2.VIEW_1…
user3667171
2
votes
1 answer

How to generate unique code in INSERT inside MERGE?

In the Target table there is a PK column usr_cd which doesn't exist in the Source table. Hence, while inserting from Source to Target I want to generate a unique code. How can I achieve this when I am using MERGE statement? Following options are…
2
votes
1 answer

SQL Server merge: If two columns in source table are not equal then insert them as new records in one column of target table

I have been grappling with an issue where i need to compare two columns in the source table and merge them into the third table as new rows if not equal. If they are equal then they are inserted as one record Example Below: Source Table: MIN_ID …
Rahul Ravi
  • 53
  • 1
  • 8