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
2 answers

MERGE INTO not working with Postgres 9.6

In my Postgres 9.6 environment, when I try to execute "MERGE INTO" query, it throws me following error: ERROR: syntax error at or near "MERGE" LINE 1: MERGE INTO Stock USING Buy ON Stock.item_id = Buy.item_id W... ^ It seems like it does…
Chintan Patel
  • 729
  • 3
  • 14
  • 31
2
votes
1 answer

SQL - An aggregate may not appear in the set list of an UPDATE statement

Hello and good day to everyone. Could you help me correct this SQL sequence? MERGE INTO table2 WITH (HOLDLOCK) AS target USING ( SELECT column1, AccessDate, AccessCount, column4, column5, column6, …
Bogdan Pușcașu
  • 545
  • 2
  • 7
  • 21
2
votes
1 answer

MERGE attempted to UPDATE or DELETE the same row more than once

I am trying to load a standard Kimball SCD2 Dimension, using a merge statement which I got from the following website: http://www.kimballgroup.com/2008/11/design-tip-107-using-the-sql-merge-statement-for-slowly-changing-dimension-processing/ This…
Erik hoeven
  • 1,442
  • 6
  • 26
  • 41
2
votes
1 answer

T-Sql MERGE statement update when criteria matches newly inserted record

This is the XML I have: 1 1 2 23 11052
HABJAN
  • 9,212
  • 3
  • 35
  • 59
2
votes
3 answers

Update violates primary key

My update receives the following error when updating to the database: ORA-00001: unique constraint (DONALDBURY.BARS_ID_PK) violated I am using the following code as my update. I am not wanting to get rid of the constraint but to find out a…
user5730489
2
votes
1 answer

Oracle SQL merge tables without specifying columns

I have a table people with less than 100,000 records and I have taken a backup of this table using the following: create table people_backup as select * from people I add some new records to my people table over time, but eventually I want to…
Rossiar
  • 2,416
  • 2
  • 23
  • 32
2
votes
1 answer

java.sql.SQLSyntaxErrorException: ORA-00947: not enough values On Insert Statement

I created following table: BEGIN EXECUTE IMMEDIATE 'CREATE TABLE "MY2XSYEHT" ( pKy NUMBER(19, 0) PRIMARY KEY AUTOINCREMENT, cTm NUMBER(19, 0), uTm NUMBER(19, 0), oTm NUMBER(19, 0), eTm NUMBER(19, 0), oID VARCHAR2(32), kID VARCHAR2(32),…
Mrinal Sharma
  • 97
  • 2
  • 10
2
votes
1 answer

How to merge 2 or more columns into one?

I have a real problem with one task in SQL concerning merging data from 2 and more columns into 1 column in the most effective way. id column1 column2 column3 1 ok notOK 2 3 abraka dabrra 4 miew …
HeadOverFeet
  • 768
  • 6
  • 13
  • 33
2
votes
1 answer

Combine two rows based on common ID

I have a query that returns two lines per ID. I would like to be able to merge these into one single row, based on the ID. There will only ever be two rows per ID. As in the example below, one row will only ever contain ColumnA and the other row…
Alec.
  • 5,371
  • 5
  • 34
  • 69
2
votes
1 answer

SQL Server Merge WHEN NOT MATCHED clause customizations

When merge clause is used in SQL Server, I need to insert a row when it is not available. This is what I have tried: drop table test; create table test (col1 int, col2 varchar(20)); insert into test values(1, 'aaa'); insert into test values(2,…
dmachop
  • 824
  • 1
  • 21
  • 39
2
votes
2 answers

T-SQL - Merge all columns from source to target table w/o listing all the columns

I'm trying to merge a very wide table from a source (linked Oracle server) to a target table (SQL Server 2012) w/o listing all the columns. Both tables are identical except for the records in them. This is what I have been using: TRUNCATE TABLE…
Jeff U.
  • 223
  • 3
  • 8
2
votes
4 answers

How to decompose the sql%rowcount of MERGE statement

Is it possible to get number of rows inserted and updated separately in merge query? SQL%COUNT gives total number of rows merged.
Vikas Kumar
  • 154
  • 2
  • 6
2
votes
2 answers

Merge two tables in PL/SQL block

I have next 3 tables: Fibonacci(id, value) first 1000 fibonacci numbers. Prime(id, value) first 2000 prime numbers. FiboPrime(value, prime), initially the table is empty. I have to populate the FiboPrime table, with the prime numbers from…
Rapidistul
  • 406
  • 4
  • 9
  • 19
2
votes
1 answer

Merge leads to casting error DATETIME --> INT

I can't figure out why this error is occurring. Can anyone else help me spot the error? I'm trying to merge into a temp table, and getting the error Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to…
Mike Kantor
  • 1,400
  • 4
  • 24
  • 45
2
votes
1 answer

Optional parameters in Merge

I have a stored procedure used for updating records in a SQL Server database. I want to use the existing value within the database if a match is found and a null is provided as the variable. Often only partial data is available so the procedure is…
madlan
  • 1,387
  • 10
  • 34
  • 63