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

SQL on-demand cache table (possibly using SQL MERGE)

I am working on implementing an on-demand SQL cache table for an application so I have CacheTable with columns Type, Number, Value Then I have a function called GetValue( Type, Number ) So I want to have a function that does the following If…
theDoke
  • 553
  • 1
  • 7
  • 18
1
vote
1 answer

Merge fails for OffsetDateTime when offset is UTC (+00:00)

I’m having an issue with HSQLDB and MERGE with the following merge logic if I use an OffsetDateTime with offset +00:00 private static final String CREATE_TABLE_SQL = "create table sample (" + "code varchar2(50), " +…
lance-java
  • 25,497
  • 4
  • 59
  • 101
1
vote
1 answer

Updating destination table based on year-week and date comparison

I have two tables - source and destination, which are similar. They look like this: CREATE TABLE [dbo].[srcTable]( [Id] [INT] NULL, [Value] [INT] NULL, [QueryDate] [DATE] NULL ) CREATE TABLE [dbo].[destTable]( [Id] [INT] NULL, …
FeodorG
  • 178
  • 2
  • 10
1
vote
1 answer

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 merging will happen every 15 mins and based on the…
Sanket Kelkar
  • 129
  • 2
  • 9
1
vote
1 answer

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 varchar(40) NOT NULL, block_nbr bigint DEFAULT 0, is_open…
Aud12
  • 23
  • 1
  • 4
1
vote
1 answer

T-SQL MERGE Performance in typical publishing context

I have situation where a "publisher" application essentially keeps a view model up to date by querying a VERY complex view and then merging the results into a denormalized view model table, using separate insert, update, and delete operations. Now…
David Boike
  • 18,545
  • 7
  • 59
  • 94
1
vote
2 answers

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 and insert some values to them as below. create…
1
vote
1 answer

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) ); INSERT INTO e_demo2_tab…
Vicky
  • 639
  • 3
  • 13
1
vote
2 answers

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 Master table as below inside a stored…
tanatan
  • 31
  • 5
1
vote
1 answer

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.updated) WHEN MATCHED THEN UPDATE SET t1.data =…
1
vote
0 answers

Creating SCD type 2 with SQL

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 customer that moved from Miami to New York. I would like to…
1
vote
1 answer

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 I have already used a partition by and where rn=1 in…
karthik
  • 185
  • 3
  • 13
1
vote
2 answers

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.d1 WHERE d.d1 IS NULL WHEN NOT MATCHED…
ZygD
  • 22,092
  • 39
  • 79
  • 102
1
vote
0 answers

Merge on multiple conditions

Is it possible to use multiple conditions on SQL merge ON condition to make as unique identifier instead of concatenating? CompanyId+StoreLocation = UniqueId. I do not want to create an additional concatenated column. Will this work? If not, what…
1
vote
1 answer

While merging in Oracle 11g, I want to delete destination row that is not matching with source

SQL> SELECT * FROM SOURCE; MEMBER_ID FIRST_NAME LAST_NAME RANK ---------- ---------- ---------- -------------------- 1 Abel Wolf Gold 2 Clarita Franco Platinum 3 Darryl Giles Silver …
uttarkar
  • 11
  • 1