A SQL statement that can conditionally insert, update, or delete target rows.

learn more… | top users | synonyms

40
votes
1answer
33k views

MERGE a subset of the target table

I am trying to use a MERGE statement to insert or delete rows from a table, but I only want to act on a subset of those rows. The documentation for MERGE has a pretty strongly worded warning: It ...
17
votes
1answer
215 views

Why does this MERGE statement cause the session to be killed?

I have the below MERGE statement which is issued against the database: MERGE "MySchema"."Point" AS t USING ( SELECT "ObjectId", "PointName", z."Id" AS "LocationId", i."Id" AS "Region" ...
11
votes
3answers
8k views

Merge statement deadlocking itself

I have the following procedure (SQL Server 2008 R2): create procedure usp_SaveCompanyUserData @companyId bigint, @userId bigint, @dataTable tt_CoUserdata readonly as begin set ...
7
votes
1answer
5k views

What are my locking options for a Merge statement?

I have a stored procedure that performs a MERGE statement. It seems like it locks the whole table by default when performing the merge. I'm calling this stored procedure inside of a transaction ...
7
votes
1answer
590 views

Merge row size overflow in SQL Server - “Cannot create a row of size..”

The target table to which I'm trying to merge the data has ~660 columns. The code for the merge: MERGE TBL_BM_HSD_SUBJECT_AN_1 AS targetTable USING ( SELECT * FROM ...
7
votes
2answers
5k views

Oracle's Merge v. Select->Update/Insert

What is the faster statement? A MERGE or Select->Update/Insert depending on if it returns a value? I'm trying to optimize a database, but I haven't found any speed tests comparing the two. I found ...
7
votes
2answers
2k views

Merge replication fails on executing query

My merge replication was working fine this morning. Since an hour or 2 ago, my synchronisation status windows keeps showing this message: Thread id 3108 will wait for 15 second(s) before retrying the ...
5
votes
4answers
536 views

How can I avoid repeating the same condition in a “when matched then update” clause?

I want to merge one table into another. I need to apply conditional logic in my WHEN MATCHED clause, which would ideally be done like this: MERGE INTO ATable AS a USING BTable AS b ON a.ID = b.ID ...
5
votes
1answer
3k views

MERGE with OUTPUT doesn't seem to be doing the right thing

I'm adding a foreign key to a table, and removing any rows that violate the FK, copying them into a ModifiedTable_invalid table. As part of the script, I've got the following MERGE command: MERGE ...
5
votes
2answers
710 views

How to use merge hints to isolate complex queries in SQL Server

I can understand that if I join individual queries that are "individually fast" the combination may become slow because the default execution plan may be non-optimal. However when I know the number ...
4
votes
4answers
11k views

How do I delete only related records in a multi-key MERGE in SQL Server?

Suppose you have something like this: Source table (variable): Values ( LeftId INT NOT NULL, RightId INT NOT NULL, CustomValue varchar(100) NULL ) Target table: Mapping ( LeftId INT NOT ...
4
votes
2answers
3k views

Can I optimize this MERGE statement?

I am attempting a single column merge between two tables. The first table (VisitorSession) has 40,000,000 rows. The second (ShoppingCart) has 9,000,000 rows. In my development environment, the query ...
4
votes
1answer
494 views

A clarification of MERGE best practices

I just wanted to ask a clarification about something on the MSDN from the "Optimizing MERGE Statement Performance" page. I am working with a data warehouse that takes in records from many different ...
4
votes
2answers
5k views

Way to do MERGE with update source when target not match?

I am trying to write a stored procedure that will take new values from a temp table, merge them into an actual table, giving me the ids of the inserted rows, and the rows that already exist matching ...
3
votes
1answer
1k views

Merge Problems SQL Server

I'm trying to create a procedure that will take the values from a temporary table, and load them into another table. The second table has a composite unique constraint across its three columns (Not ...
3
votes
1answer
95 views

how would i merge a column where matches a different column? [closed]

It's very similar to the below post How to merge column in mysql depending on match table user I would like to Merge customer_id from table user to table cust where customers_firstname AND ...
3
votes
1answer
138 views

3 table merge with some condition

I am new to this forum and I am learning mysql with the help of online resources, currently I am trying to compare and merge table with some conditions Fiddle These are my tables; mysql&...
3
votes
2answers
3k views

Merging two backups form a postgresql database

I have a kind of big postgresql database and two backup files (each uncompressed dump file ~56 mb) representing 2 states of the data in time. 1st state - backup from a couple days ago 2nd state - ...
3
votes
2answers
4k views

Merge two records in the same table, keeping foreign key relationships intact for both

I have a table, called SITES, that has three columns, lets say it looks like this: ID Name Path 1 Google http://www.google.com/ 2 Microsoft http://www.microsoft.com/ I also have ...
3
votes
1answer
95 views

Merge causing deadlocks

We are running MS SQL Server 2008 R2. We've got a VendorMaster table (with about 8700 rows) that we are trying to keep in sync across two databases using a Merge statement. The merge code runs every ...
3
votes
1answer
47 views

How to maintain trees and their roots in SQLite

I have a (SQLite) database with a table representing nodes in a forest (set of trees): create table "Node" ( "id" Text primary key, "parent" Text references "Node" ("id") -- parent node ...
3
votes
0answers
102 views

SQL Server MERGE statement efficiency

We are using the following query to merge new data between tables. -- Merge descriptions from Sync_CheckPointDescriptions table with temporary table #temp MERGE CheckPointDescriptions AS TARGET ...
3
votes
0answers
131 views

Once in a while synchronization of the independently updated mysql databases problem

I have a production database and a test database running on MySQL with MyISAM and InnoDB tables, which are updated independently. The Production database is updated by system and sometimes by a user. ...
3
votes
1answer
96 views

Does MERGE updates when not needed?

In SQL Server 2012, Does a merge statement like this : WHEN MATCHED THEN update SET A.len = B.len updates a row even if A.len equals to B.len ? Or does the SQL SERVER knows to ignore this ...
2
votes
2answers
330 views

MERGE command in SQL Server 2005

I have been trying to develop a MERGE in SQL Server 2005. It does not have all features yet, because I have been struggling in "how to put the merge logic WORKING AS A SET, rather than rows". ...
2
votes
2answers
48 views

Using Variables to contain column names for SQL Server Merge statemtns

I'm trying to automate the column list grab for a merge job, and running into a bit of a head scratcher. I don't want to have to manually type out all of the column names, and thought I may be able to ...
2
votes
1answer
283 views

How to merge 2 tables in Oracle

I have 1 big table with 25 milions of rows and i have to merge data of another table with 500,000 rows into the big table. the rows of both tables have the same columns and i have to check if the key ...
2
votes
1answer
65 views

Update the value when the row already exists

I have table 'A' in schema 'USER'. There are three columns in this table. below are the names of the columns: id (primary key) name age I want to insert a row in this table, but if the row is ...
2
votes
1answer
2k views

moving a Partition of Partitioned Table to other File Group, SQL Server

I created partitioned table on DateTime column. for last 6 month. I placed 3 oldest month on a file group named 'FG_OlderData', and 3 newest month on a file group named 'FG_NewerData'. Partition ...
2
votes
1answer
507 views

Merge Statement + Replication difference for SQL Server 2012

We have a SSIS package that will pull from a source database and run a merge statement to put the data into a "Master" database. (Eventually the "Master" database will be the source of truth, but we ...
2
votes
1answer
213 views

Help with best practice of merging databases

Bad news, our website server (Windows 2003) crashed because of dead RAID controller. Luckily few hours later our backup server was up and website was live again. Hopefully tomorrow our original ...
2
votes
1answer
1k views

oracle merge operation hangs (or loops); query tuner claims it's a cartesian product operation

I have a Merge query (open to better suggestions) that seems to cause the server to hang. (I'm not sure what it's doing, but it pegs the CPU and never completes) When I run it through the query ...
2
votes
1answer
51 views

SQL Merge Statement Spending Large Amount of Time Sorting on Primary Key

We are looking to use the SQL MERGE statement in SQL Server 2012 to handle data replication for a data warehouse that we have access to from the manufacturer of a 3rd party application we use for data ...
2
votes
1answer
64 views

Conflict resolution in versioned tables

I'm trying to merge two workspaces that contain version-enabled tables. I can't figure out how to resolve conflicts, because I have a lot of rows with the same ID value. They differ only in time ...
2
votes
0answers
74 views

Merging Databases with Same Schema

I have been developing a (MySQL) database in the cloud for the past few months to link in to a web based service. Following a change to the roadmap the initial clients will be running on standalone ...
1
vote
1answer
601 views

MERGE with IDENTITY_INSERT ON does not work if Identity is not the primary key

I frequently use MERGE statements and am quite familiar with it. Now I ran into a situation where some tables have IDENTITY columns that are not the primary key. In this situation the script failed ...
1
vote
1answer
125 views

Shorthand for on primary key?

Is there a shorthand for merging rows based on the primary key? For example: MERGE INTO [MyDatabase].[MySchema].[MyTable] AS target USING [MyLinkedServer].[MyDatabase].[MySchema].[MyTable] AS source ...
1
vote
2answers
746 views

Stored procedure - problem with merge - Insert

I am calling a stored procedure from my c# code . The procedure works fine for update, but fails for Insert. Is the following merge-insert procedure code correct? Any help is appreciated. Here is ...
1
vote
2answers
47 views

How can I merge duplicate rows in a single table?

I have a table with duplicate barcodes (barcode) and carts (cart) and (Quantity_counted_IN) and (Quantity_Counted_out). I want to merge all duplicate records that have the same barcode and cart ...
1
vote
2answers
58 views

Getting Only What Changed from SQL Server 2005

We have a 17 store system which are on SQL Server 2005 Standard Edition and on Windows 2003 server. We currently have the following process: At each store we have an SQL job which runs a procedure ...
1
vote
3answers
446 views

Merge replication throws PRIMARY KEY violation during the initialization of Subscriber

I am using SQL Server 2014 SP1 to set up merge replication on Windows 2008. The databases (Publisher & Subscriber) are restored from the production back up of SQL Server 2005 publisher database. ...
1
vote
1answer
664 views

How do I update and insert specific set of rows to an existing table using Merge in SQL Server 2008 R2?

I have a table in the database that I need to update based on the records being passed to it. I am given a table-valued-parameter that has the same schema as the existing table (for all intensive ...
1
vote
2answers
2k views

Merge faster than Insert

Are there any advantages/disadvantages of making use of a merge over a insert function? Can an insert statement make use of the `OUTPUT' Note: the ON clause for the merge is set to 1=0 (always insert)...
1
vote
1answer
208 views

Query hint? for deduplicated CTE in Merge

Referring to this link: Optimizing MERGE Statement Performance The following criteria are given for ideal performance: Create an index on the join columns in the source table that is unique and ...
1
vote
1answer
167 views

Merge a table into an existing one

I know how to JOIN two tables as create a new table out of the results, using CREATE TABLE result_table AS ( SELECT * FROM tableA JOIN tableB ). I forgot to include some columns from the JOIN in the ...
1
vote
2answers
634 views

Update row where associated row does not exist

Given the table named ROLE: role_id[PK] NUMBER role_name VARCHAR2 Using only SQL (no PL/SQL) how would I write a MERGE (or UPDATE?) statement that would update the role_name as below, but only ...
1
vote
1answer
1k views

Multiple table rows in one row Informix

I have problem just like this one but in Informix. I have table with 2 attributes ID, email like this one: ID email 1 my_mail1@mail.com 2 my_mail2@mail.com 2 my_mail3@mail.com 2 my_mail4@...
1
vote
1answer
478 views

Merge Fields into one Row with a Query

I am working with Microsoft Office Professional Plus 2010. What I am trying to figure out is how to merge all characters for each DOMAIN field into one row, for example: Q01 ...
1
vote
1answer
2k views

Problem using merge into a remote table over dblink

I'm attempting to perform a merge into a remote table, let's say on server A (over a dblink), using a local table on server B. Source and target databases are both on Oracle 10.2.0.3. We already have ...
1
vote
1answer
295 views

Equivalent of MRG_MYISAM in databases other than MySQL? [closed]

Does anyone know if other database have something equivalent to MRG_MYISAM (aka the MERGE table type/storage engine)? I know about fragmenting, but this is not quite the same AFAIK. We're using ...