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.
317
questions
0
votes
2
answers
41
views
oracle "merge into" too slow
i'm trying to update a column in a table using the id of another table only if one or two field match each other. Sadly the query run very slowly and i don't understand why.
PS:(the checked fields for ...
1
vote
1
answer
49
views
Merge Query With Delete From Target Condition
I am working on a task where my source is AWS RDS - SQL Server and my target is Azure SQL Server.
There's a table with 80M records in my source that needs to be merged with my target table.
This ...
0
votes
1
answer
31
views
Backfill data based or insert new records based on matching criteria
I have the following effective dated table with several thousands records that would need to be updated/inserted based on certain criteria.
Table Car_Specification:
Effective_Date
Car_ID
Num_Wheels
...
1
vote
1
answer
48
views
Merge statement from H2 database is throwing a "Column count does not match" error
I'm trying to replace Postgres's "on conflict (..) do update.." with a merge statement on an H2 database, I'm however facing an issue I'm unable to figure out.
create table shop (
name ...
0
votes
1
answer
25
views
PySpark Delete Specific Rows Without Primary Key
I am trying to make up for PySpark's lack of 'WHEN NOT MATCHED BY SOURCE' in MERGE statements. I want to delete rows from a table when there are no matching rows in another table. However, my table ...
0
votes
0
answers
49
views
Why use MERGE statements that only do 1 operation in SQL?
In our code base I see a lot of SQL MERGE statements that only perform an UPDATE or only perform an INSERT or only perform a DELETE. I am having a hard time understanding why developers don't just ...
0
votes
0
answers
33
views
BQ - Merge command inserts more than one row
Ran the following query which I expected to insert or update only one row. But the query inserted 24923 rows which is the total number of rows existing in the table at the time of running this command....
2
votes
1
answer
38
views
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 ...
0
votes
0
answers
276
views
DBT Merge Partition Prunning
This is my first time working with dbt! I have successfully implemented an incremental model using #db-bigquery with following config like so
{{ config(
materialized='incremental',
alias='...
3
votes
1
answer
180
views
Why merge-delete-trigger causes ORA-30926: unable to get a stable set of rows in the source tables?
Prepare the schema to reproduce the issue (on db<>fiddle):
create table t (id int, val int, modified timestamp default systimestamp)
/
create or replace trigger trigg_on_t
before update on t ...
0
votes
0
answers
60
views
SQL MERGE with variable number of values
I would like to know if a MERGE statement could, somehow, be written to support value sets with different column counts?
First, if we had a table that is defined as follows:
CREATE TABLE [Example]
(
...
0
votes
1
answer
455
views
Executing a stored procedure as a result of merge statement
I am using a MERGE statement in a stored procedure that looks like this:
MERGE Sections AS sTarget
USING @Sections AS sSource
ON sTarget.SectionID = sSource.SectionID
...
3
votes
1
answer
112
views
Lock level on MERGE statement
Does anyone know what is the lock level (row, table) on PostgreSQL MERGE command? I tried to dig deep on the documentation but I couldn't find anything that could clarify my mind. If anyone have a ...
0
votes
1
answer
551
views
Merge into table with 'with' statement?
I need to update or insert to table range of dates with some data. For example, I get startDate = 01/01/2022 and endDate = 01/01/2023, and I need to add 365 rows on this table or update the relevant ...
0
votes
1
answer
188
views
Convert MERGE to UPDATE
I have the following MERGE statement.Is there a way to convert this into an update statement without using MERGE?
MERGE INTO tab1
USING (SELECT tab1.col1, tab2.col2
FROM tab1, tab2
...
1
vote
2
answers
139
views
Snowflake: unable to reference column in target table inside a case predicate for notMatchedClause in a MERGE command
It seems like I am able to reference a column the target table in a case predicate for a matchedClause in a MERGE command, but am unable to do so in a notMatchedClause.
For example I create two tables ...
1
vote
1
answer
119
views
How to add two conditions in merge statement using rowid and rownum
CREATE SEQUENCE e_demo2_tab_sq;
CREATE TABLE e_demo2_tab
(
tab_id NUMBER(10) DEFAULT e_demo2_tab_sq.nextval NOT NULL,
e_id NUMBER(10),
e_uuid NUMBER(10),
seq_cnt NUMBER(10)
)...
0
votes
2
answers
212
views
How to Update value for multiple Field in Oracle SQL
I have to update field: Number, Tgl, product in Table Detail_product from table Details
with condition:
If, id on Table Detail Product an Detail is same then
update Detail_product.Tgl using Details....
0
votes
0
answers
15
views
Merging two tables - one ID can have multiple address
I have to write a stored procedure that merges two tables. The one is a staging table, and the other our main table. The problem is one user can have more than one address. There is a column for when ...
2
votes
2
answers
1k
views
SQL Server: how to delete rows with matching ID that were not affected by MERGE?
I have a table with an auto incrementing id and a unique constraint across two columns, keycol1 and keycol2.
Suppose the table has this data:
H| (id, keycol1, keycol2, col1, col2)
| (1, 'A', 'B', ...
7
votes
0
answers
518
views
Why does a merge into a temporal table with a nonclustered index in the history table throw an error
I get the following error when I try to merge under a few conditions. Is someone able to explain why? Seems like it's a problem with SQL Server itself, but I wanted to post it here to confirm.
...
0
votes
1
answer
513
views
Update multiple columns based off another table in DB2 V5R4M0
I am working on an update statement for an AS400 DB/2 table. It needs to have multiple rows updated using data from another table with matching fields. Our version of the driver displays is: 05.04....
0
votes
1
answer
58
views
How to write case expression inside merge statement to print the desired result
CREATE TABLE DELIGATE_DETAILS_MAIN
( E_ID NUMBER(10,0),
COMPLETED_DATE TIMESTAMP (6),
CONSTRAINT PK_DELIGATE_DETAILS_MAIN PRIMARY KEY (E_ID));
Insert into ...
0
votes
1
answer
242
views
How to deal with SQL Merge Error Msg 8672
This seems to be a very common error that most people get when working with SQL merge statements. I too have faced this many times, and usually I do find duplicate records in my source. After ...
0
votes
0
answers
184
views
SQL Insert/Update using Merge for multiple scenarios
I have 4 scenarios to cover so I am using the below Merge query in SQL Server 2016. Using Merge query, scenarios 1,2,3 work but scenario 4 does not work. Can someone please help?
also, I am ok to use ...
0
votes
1
answer
2k
views
IF EXISTS and MERGE Statement
I have data flowing into one table from multiple other tables lets say: Table_A
Then I have a Merge stored proc that takes data from table A merges it with Table B.
However, something doesn't seem to ...
1
vote
2
answers
93
views
How to Limit the records
I have two tables called daily and master. Daily table is truncated every day but Master table holds the data and is never truncated. Every day I run a SQL script to merge the Daily table data with ...
0
votes
2
answers
260
views
SQL Merge SOURCE order and OUTPUT order
Consider an SQL MERGE statement merging 4 rows using a derived table into Target like so:
MERGE Target AS T USING
(VALUES
(?, ?, ?),
(?, ?, ?),
(?, ?, ?),
(?, ?, ?),
) as S(Account_ID,...
1
vote
1
answer
279
views
Using index in merge update in Oracle
I have query like this:
MERGE INTO table1 t1 USING
(SELECT t2.id ,
t2.updated ,
t2.data
FROM table2 t2) sel ON (sel.id = t1.id
AND sel.updated = t1....
0
votes
1
answer
313
views
DB2 - SQL - MERGE - An unexpected token -
I am a beginner here so soryy for mistake :-(
I try to execute Marge but unfortunately keeps getting the same error and I don't know what I'm doing wrong.
MERGE INTO TB_CLIENT_ACCOUNT_LINK as A
...
0
votes
1
answer
80
views
Updates and inserts by improving ON/GROUP By Clause
Here is the sample of the Source and target table. I only have one unique id called "Premise_ID". I would like to update the target table based on Premise ID. When i am running below ...
0
votes
2
answers
860
views
How to update source table when matched or not matched?
I have 2 table, my goal is when I upload file(the file contain more than one data) I already insert file data in table history, the first step is I want to compare history table to table called EDC ...
0
votes
1
answer
121
views
Can I detect if a 'merge into' H2 statement was using insert or update internally?
Is there any way to detect in Java that a H2 merge into statement was doing insert instead of update?
In both cases the result is the number of affected rows but I need to know if the record was ...
0
votes
0
answers
169
views
Oracle merge statment facing deadlock issue
I have a simple merge statment given below. But facing deadlock issue sometimes.not sure how to fix it. Is any change required in my code. kindly suggest.
MERGE INTO test1 trgt USING (
SELECT
...
0
votes
0
answers
629
views
MERGE with Postgresql 11
Wanted to know if PostgreSQL 11 does support MERGE or not.
I read on following articles -
https://wiki.postgresql.org/wiki/SQL_MERGE
https://wiki.postgresql.org/wiki/SQL_MERGE_Patch_Status
Path ...
1
vote
0
answers
37
views
Creating SCD type 2 with SQL [duplicate]
I am trying to create a SCD type 2 table in SQL server for a data warehouse.
I am using the MERGE statement, and my problem is with the "When Matched" part.
For example: I have a certain ...
0
votes
0
answers
40
views
Merge Table - Oracle
So I have this table with hierarchy data
bi_ods.table1 (source)
part1
part2
part3
1A
2A
3B
1B
1A
3B
Truncating and inserting on a stored procedure, the information is coming from some other ...
0
votes
2
answers
674
views
SQL Server MERGE statement in Postgresql?
merge into Mytable WITH (HOLDLOCK) as dst
using (select @mId as MId,
@up as UP,
@termsAccepted as TermsAccepted,
...
0
votes
1
answer
141
views
SQL merge statement with Joins
I am trying to use a merge statement for upsert operation. I want to do something like below where I join the both target and source table to the Student table for a unique key that only exists in the ...
0
votes
2
answers
437
views
SQL Error: ORA-38104: Columns referenced in the ON Clause cannot be updated
MERGE /*+ GATHER_PLAN_STATISTICS*/
INTO ATM_REQUEST ATM
USING ATM_STATUS_VIEW ST
ON ( ATM.accountno = ST.accountno
AND atm.status IS NULL
AND atm.remarks IS ...
0
votes
1
answer
379
views
Merge Statement is causing performance issues in oracle
I am trying to update using merge statement in oracle and the source has 20M records and the target has 118 M records. The performance of the Merge is taking a very long time. Listed below is my Merge ...
1
vote
1
answer
275
views
ORA-30926: unable to get a stable set of rows in the source tables when running Merge Query
I am executing a merge query to update 2 columns in a table, but I get the following error "ORA-30926: unable to get a stable set of rows in the source tables.
When I execute the merge query but ...
0
votes
1
answer
121
views
How create procedure to update everyday
I need to change this script so that it updates data every day, because insert new data every day
MERGE INTO t_customer_statistics tcs
USING (
SELECT rowid rid,
LEAD(TRX_DATE, 1) OVER (...
0
votes
1
answer
44
views
Optimize Merge query inside n^2 loop
I've got a merge query that needs to be executed one time for each combination of day and sessionType inside the request ArrayList. I am using nativeQuery to execute it.
MERGE INTO TABLE_A A
USING ...
0
votes
1
answer
54
views
How to integrate data incrementally in PL/SQL
How can you incrementally insert data from two different table into another one ?
Let's say I have table A and B and I want to insert rows incrementally into table C. Rows should only be inserted once ...
0
votes
1
answer
482
views
T-SQL : merge insert old and new value then make changes
It's my first time working with T-SQL Merge and I have been trying to write a Stored Procedure using with the following condition:
If record is present then insert into Changes table the Id, old ...
0
votes
1
answer
573
views
Merge not inserting new values
I'm trying to use MERGE to insert new values to a table only if they don't already exists in the same table.
This is the query I am using:
MERGE [dbo].[TARGET_TABLE] AS Target
USING
(SELECT [NAME]
...
1
vote
2
answers
594
views
Oracle MERGE rewritten to PySpark. If null - update, otherwise - insert
These are my tables:
destination
new_data
In Oracle SQL I can do this:
MERGE INTO destination d
USING new_data n
ON (d.c1 = n.c1 AND d.c2 = n.c2)
WHEN MATCHED THEN
UPDATE SET d.d1 = n....
0
votes
1
answer
110
views
Merge syntax, parentheses when using subquery
According to the official Merge syntax, parentheses are not needed with a subquery in the USING clause. Yet the examples always have them and if you try it without you get an error message. Is this an ...
0
votes
1
answer
561
views
Do not Update the Values in Merge statement if old values do not change while update in Merge
MERGE PFM_EventPerformance_MetaData AS TARGET
USING
(
SELECT
[InheritanceMeterID] = @InheritanceMeterPointID
,[SubHourlyScenarioResourceID] = @SubHourlyScenarioResourceID
,...