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
1
vote
1 answer

SQLite3: merge rows with common columns

For some context I have a table in SQLite3 that currently looks like this: What I am looking to do is merge rows with the same breed. The same columns will not be populated in both cases. So far I have tried this kind of query but it doesn't really…
James Harrison
  • 323
  • 4
  • 12
1
vote
1 answer

Merge Statement SQL Syntax

I am trying to compare 2 ID's from different tables and if they don't match the row that isn't in the source should be added. But I am getting this error: PLS-00103: Encountered the symbol "Person" when expecting one of the following: : = ( @ % ;…
user13475995
1
vote
1 answer

How can I add two if statements to a merge in oracle

I want to insert a row in a table if the keys are not in the table. If the keys are already in the table, I want to update two timestamps on the row as shown below: Inputs to this routine include lEarliest = TIMESTAMP lLatest =…
Jared
  • 73
  • 6
1
vote
2 answers

MERGE INTO table containing AUTO_INCREMENT columns

I've declared the following table for use by audit triggers: CREATE TABLE audit_transaction_ids (id IDENTITY PRIMARY KEY, uuid VARCHAR UNIQUE NOT NULL, `time` TIMESTAMP NOT NULL); The trigger will get invoked multiple times in the same…
Gili
  • 86,244
  • 97
  • 390
  • 689
1
vote
3 answers

How do I merge two tables in MySQL and where table 1 is primary

How do I merge two tables in MySQL? I've looked at several other posts on this topic but they don't go into enough detail for me. I'm a novice MySQL user, so bear with me I have a primary table and a temp table that look like this: CREATE TABLE…
Brad
  • 2,237
  • 5
  • 41
  • 69
1
vote
0 answers

Data Integrity bug query fix to rewrite the sql

I have a table and in this table i have data which has data integrity issue, since this is a dimension table we need to maintain the effective_dt_from and effective_dt_to and version correctly. This is the table and sample data: create table TEST ( …
Data2explore
  • 452
  • 6
  • 16
1
vote
1 answer

SQL: Trigger is not collecting every row

I have problem with trigger that should update one table when there i new row in other table. I have two tables: First: SELECT [Id], [Timestamp], [MachineName], [StatusId], [Quantity] FROM [dbo].[Events] And second one: SELECT [Id],…
maidey
  • 15
  • 3
1
vote
1 answer

Oracle merge sql doesn't work when update with where condtions

My problem is that an error occurred when I update with where conditions. The error message is 00936. 00000 - "missing expression" If I remove this line where q1.SALE_QTY <> -9 , then that sql works. Database version : oracle 9i SQL: merge into…
mike.jiang
  • 207
  • 1
  • 6
  • 16
1
vote
1 answer

Is SQL merge good replacement for single-table select-check-update statements?

I have table adv_days(adv_date date not null primary key, edit_state integer) and I want to do series of commands: check wether '27.11.2019' exists in table, if exists then update edit_state to 1, if does not exist, then insert date '27.11.2019'…
TomR
  • 2,696
  • 6
  • 34
  • 87
1
vote
0 answers

PostgreSQL equivalent of SQL merge trigger and INSERTED table

I am new to database triggers/PostgreSQL and trying to convert the following SQL trigger to PostgreSQL. SQL script : CREATE TRIGGER tr_EmpMerger ON Emp INSTEAD OF INSERT AS BEGIN MERGE INTO Emp AS Target USING ( SELECT * FROM INSERTED ) AS…
1
vote
4 answers

Increase performance of multi-join Merge including Update

I would like to update a column based on joins. Unfortunately, the tables that I am updating and merging are rather large: 87,220,021 rows. Here is the content of the table (that I am merging with itself): ID ID_VERSION VALUE_1 …
Arne
  • 57
  • 6
1
vote
2 answers

Convert MERGE statement to UPDATE statement

MERGE INTO TABLE1 t1 USING TABLE2 t2 ON (t1.ID = t2.ID) WHEN MATCHED THEN UPDATE SET t1.PHONE_NUMBER = CASE WHEN t1.type in ('A','B') THEN t2.phone_number ELSE NVL(t2.phone_number,…
gooner_psy
  • 77
  • 1
  • 11
1
vote
0 answers

How to get target table in sync using Merge

How to get table in sync using Merge. I have two tables: Source and Target. I want Target table to be in sync with Source after the query has completed. Following are the columns for both tables. I want all columns except Contact_no to be in sync…
1
vote
1 answer

How to INSERT non-duplicated, can I use MERGE with self-reference?

I need to insert non-duplicated values... This "merge with self-reference" is not working as expected, but not generated a error message... Can I use similar thing? MERGE INTO mydb.mytab AS Tref USING mydb.mytab AS T ON Tref.id=T.id WHEN NOT…
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
1
vote
1 answer

How do i update multiple records using MERGE statement and use max(column_value) based on previously updated records in the same statement?

I need to use the Oracle MERGE statement to update records of the table. One of the column I would like to update should be derived from MAX value of both existing records of the table and the records that are updated as a part of the current MERGE…
Raghu
  • 13
  • 5