sql-merge

Update and Insert When Condition is Matched in TSQL-Merge

岁酱吖の 提交于 2020-01-02 07:32:15
问题 I have been trying to Write a Stored Procedure where i can perform UpSert using Merge with the Following Condition If Record is Present then change EndDate of Target to Yesterday's day i.e., Present Day - 1 If Record is not Present then Insert New Record Here is the Table tblEmployee i used in SP CREATE TABLE tblEmployee ( [EmployeeID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](10) NOT NULL, [StartDate] [date] NOT NULL, [EndDate] [date] NOT NULL ) Here is my SP which Takes UDTT as Input

Is Merge and Merge join same in SQL Server?

我是研究僧i 提交于 2020-01-02 05:33:06
问题 What is the difference between Merge and a Merge Join in SQL Server? 回答1: MERGE is a DML statement (data manipulation language). Also called UPSERT (Update-Insert). It tries to match source (table / view / query) to a target (table / updatable view) based on your defined conditions and then based on the matching results it insert/update/delete rows to/in/of the target table. MERGE (Transact-SQL) create table src (i int, j int); create table trg (i int, j int); insert into src values (1,1),(2

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

懵懂的女人 提交于 2019-12-23 03:12:40
问题 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 temp_import ( id int(11) NOT NULL auto_increment, Name varchar(255) default NULL, MerchantID int(11) default NULL, SKU varchar(255) default NULL, PRIMARY KEY ( id ) ) ENGINE=MyISAM AUTO_INCREMENT=765811 DEFAULT CHARSET=utf8; I'm inserting data into the

SQL MERGE to remote (linked) server table

白昼怎懂夜的黑 提交于 2019-12-21 07:55:52
问题 Is it possible to utilize the SQL MERGE function on a linked server's database table? The end goal is to synchronize the remote table with our local SQL server table. I’ve done some research online and couldn’t find any related information. If it is possible how would you setup the source and target statements? 回答1: To reiterate the comment by @Mikael Eriksson, yes, you can. The target of a MERGE cannot be remote, but the source of a MERGE can be remote. So, if you can run the MERGE statement

UPDATE-no-op in SQL MERGE statement

孤街醉人 提交于 2019-12-21 03:17:13
问题 I have a table with some persistent data in it. Now when I query it, I also have a pretty complex CTE which computes the values required for the result and I need to insert missing rows into the persistent table. In the end I want to select the result consisting of all the rows identified by the CTE but with the data from the table if they were already in the table, and I need the information whether a row has been just inserted or not. Simplified this works like this (the following code runs

UPDATE-no-op in SQL MERGE statement

拟墨画扇 提交于 2019-12-21 03:17:06
问题 I have a table with some persistent data in it. Now when I query it, I also have a pretty complex CTE which computes the values required for the result and I need to insert missing rows into the persistent table. In the end I want to select the result consisting of all the rows identified by the CTE but with the data from the table if they were already in the table, and I need the information whether a row has been just inserted or not. Simplified this works like this (the following code runs

MERGE table, do nothing when matched

橙三吉。 提交于 2019-12-18 08:25:58
问题 I have a table DOMAINS in 2 different schemas with columns ID , NAME , CODE , DESCRIPTION . For any NAME exist in new schema, it should use existing ID without any merge; for those new NAME records, it should insert with ID from old schema. MERGE INTO DOMAINS A USING (SELECT ID,NAME,CODE,DESCRIPTION FROM <Old Schema 6.1>.DOMAINS@DB_MIG_61_TO_74) B ON(A.NAME = B.NAME) WHEN MATCHED **<do nothing>** WHEN NOT MATCHED THEN INSERT(A.ID,A.NAME,A.CODE,A.DESCRIPTION) VALUES(B.ID,B.NAME,B.CODE,B

MERGE table, do nothing when matched

折月煮酒 提交于 2019-12-18 08:25:33
问题 I have a table DOMAINS in 2 different schemas with columns ID , NAME , CODE , DESCRIPTION . For any NAME exist in new schema, it should use existing ID without any merge; for those new NAME records, it should insert with ID from old schema. MERGE INTO DOMAINS A USING (SELECT ID,NAME,CODE,DESCRIPTION FROM <Old Schema 6.1>.DOMAINS@DB_MIG_61_TO_74) B ON(A.NAME = B.NAME) WHEN MATCHED **<do nothing>** WHEN NOT MATCHED THEN INSERT(A.ID,A.NAME,A.CODE,A.DESCRIPTION) VALUES(B.ID,B.NAME,B.CODE,B

MERGE in Entity Framework

折月煮酒 提交于 2019-12-18 04:44:12
问题 Is there a way to call T-Sql's MERGE command from .NET Entity framework 4? 回答1: No there no such built-in functionality - you must build your own. Very common is for example approach like: public void SaveOrUpdate(MyEntity entity) { if (entity.Id == 0) { context.MyEntities.AddObject(entity); } else { context.MyEntities.Attach(entity); context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); } // You can call SaveChanges here or you can call it separately after multiple

Merge statement and after triggers on target table

混江龙づ霸主 提交于 2019-12-12 14:00:09
问题 I have two after triggers on target table (one for insert and one for update). Now if I execute merge on the target table, the triggers are executed only once. Although the merge statement executes around 300 updates, and 200 inserts. I checked it with print statements in each trigger, right after getting data from deleted, inserted record into variables. How come? Is this a bug? I have SQL Server 2008 sp1 std (part of the SBS2k8). 回答1: A trigger runs per single action. Not "per row" You have