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
0
votes
1 answer

Need to convert Oracle "merge into" query to PostgreSQL

I'm trying to convert the following Oracle query to PostgreSQL: MERGE into feepay.TRPT_W2_REPORTS TRPT1 USING( WITH RWS AS (SELECT PROG.BINCLIENT, TRPT.PUT_DIRECTORY FROM feepay.program2 PROG INNER JOIN feepay.TRPT_W2_PROGRAMS…
radjiv
  • 43
  • 4
0
votes
1 answer

How to use table input parameter for MERGE command in Oracle Stored Procedure

I am currently trying to write a stored procedure that inserts or updates multiple rows into my database. I pass the rows via a table input parameter, but I am stuck on how to pass those rows to the MERGE-command. This command is working fine: MERGE…
Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
0
votes
1 answer

Merge condition not working in oracle when trying to merge

I have a table as: -------------- |id descrip | |1 A | |2 B | |3 C | |3 D | |3 E | |4 F | |4 F | My expected output is: -------------- |id descrip | |1 A | |2 B | |3C C …
AB21
  • 353
  • 1
  • 4
  • 15
0
votes
2 answers

Oracle merge statement not reaching NOT MATCHED part

The following Merge statement is not Inserting (going to NOT MATCHED) the record. The query used in the Condition part of the Merge clause sometimes will not return any row. Is this causing the Insert to not happen? MERGE INTO apps.test_table ab…
Gautam Hans
  • 135
  • 1
  • 12
0
votes
1 answer

SQL Merge - Only insert where specific column has been updated

I have a merge statement that looks like the below ... WHEN NOT MATCHED BY TARGET then Insert (ID, Status, Read, User, ChangeDate) VALUES (Source.ID, Source.Status, Source.Read, Source.User, Source.ChangeDate) ... I am currently using the ID,…
Jess8766
  • 377
  • 5
  • 16
0
votes
2 answers

SQL Merge statement will not accept some of my identifiers

Hi i have created the following SQL statement to help me merge data from one table into another table. The following code seems t o be working fine. MERGE INTO FTOURSTATS ts USING (SELECT * FROM guest_tour) t ON (t.TOUR_ID = ts.TOURID AND…
Steve Rogers
  • 63
  • 1
  • 7
0
votes
1 answer

Oracle SQL, merge into: add condition only where there are multiple matches

My merge into cannot run because the 'on' condition does not identify a one-to-one correspondence between the tables. I would like to solve this by asking that, only on such rows where the condition fails, the value of a third column is used to…
Giuseppe
  • 518
  • 10
  • 22
0
votes
2 answers

MERGE with WHERE clause

Consider data existing in a table: Customers | CustomerID | Name | Status | |------------|-----------------|--------------------| | 1 | Ian Boyd | Killed | | 2 | Shelby Hawthorn | Booked …
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0
votes
2 answers

Understanding SQL Merge statement?

I have a source table that has data identical to my target table. When I try to run a merge statement, it fails with the error merge can't update a target row multiple times. So My Question is since they are identical why SQL did succeed but…
sesy
  • 75
  • 8
0
votes
0 answers

MSSQL Merge when not matched THEN Insert ID From Source

I've got problem with MERGE Statement. I am comparing 2 tables. WHEN MATCHED UPDATE works like charm, but there's a problem with WHEN NOT MATCHED THEN INSERT clause. I want to insert rows to daSRC (source) table but I need to have the same da_DocId…
mr R
  • 997
  • 2
  • 12
  • 25
0
votes
3 answers

Update columns using conditions - Oracle SQL

I have a table1: u_a_id d_c_s c_nm c_seq r_c_p 1 908 Test1 1 20 10 908 Test1 1 21 11 908 Test1 1 12 12759 908 Test1 1 31 12759 908 Test1 1 32 12861 878 …
dang
  • 2,342
  • 5
  • 44
  • 91
0
votes
1 answer

Update records based on select query

I have a table1: u_a_id d_c_s c_nm c_seq r_c_p 1 908 Test1 1 20 10 908 Test1 1 21 11 908 Test1 1 12 12759 908 Test1 1 31 12759 908 Test1 1 32 12861 878 …
dang
  • 2,342
  • 5
  • 44
  • 91
0
votes
1 answer

SQL Merge only inserting rows, not updating

In SQL Server 2016, I'm trying to create/update records in my target table using Merge, but I think I'm running into a limitation where Merge only makes one pass through the data, so am getting all inserts and no updates. In the target table (which…
VBStarr
  • 586
  • 6
  • 17
0
votes
1 answer

merge sql condition is null issue

I have two very similar SQL statements. On of them work and on not. The SQL error message seems to be misleading. Can you figure it out? SQL 1 -- this works just fine Merge into t1 Using ( Select art_ref_nr, channel, some_value…
Peter Frey
  • 361
  • 4
  • 17
0
votes
1 answer

SQL Merge update issue

I am trying to increment a project code when I do a SQL merge operation I have two tables. One has lots of information including customer's and projects. I want to merge the customer name and project name from one table to the other. This article…
jt_de
  • 71
  • 6