How to merge from source table to destination table but in source table there is no primary key
how can I insert,update,delete the data from source to destination table
How to merge from source table to destination table but in source table there is no primary key
how can I insert,update,delete the data from source to destination table
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 24, 2023, 6:16 AM EDT
To perform an insert/update/delete operation on the destination table based on the data from the source table, you can use the following steps:
Use a query to identify the matching records between the source and destination tables. This can be done by comparing the values in one or more columns that are common to both tables. For example, if the tables have a common "Name" column, you can use that column to match records between the tables.
For the records that match between the source and destination tables, perform the appropriate action based on whether the record already exists in the destination table or not.
It's important to note that without a primary key or a unique constraint on a column(s), there may be duplicates or inconsistencies in the data after the merge. Therefore, it's recommended to create a primary key or a unique constraint on the destination table before performing the merge.
Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Mar 25, 2023, 2:06 AM EDT
You can use this
MERGE Data2 AS Target USING Data AS Source ON Source.Abc= Target.A_Abc and Source.cde= Target.A_cde -- For Inserts WHEN NOT MATCHED BY Target THEN INSERT (A_Abc,A_cde, A_efg) VALUES (Source.Abc,Source.cde, Source.efg) -- For Updates WHEN MATCHED THEN UPDATE SET Target.A_Abc = Source.Abc, Target.A_cde = Source.cde Target.A_efg = Source.efg -- For Deletes WHEN NOT MATCHED BY Source THEN DELETE;Jaya PrakashRank #1,293 · 1.3K reputationPosted Mar 24, 2023, 1:52 AM EDT
Pls Take Similar Values which u have passed in the first table u should need to match the values with 2nd table
Meghana MRank #8,429 · 222 reputationPosted Mar 24, 2023, 9:35 AM EDT
source class
public class Data
{
public string Abc { get; set; }
public string cde { get; set; }
public string efg{ get; set; }
}
destination class
public class data2
{
public string A_Abc { get; set; }
public string A_cde { get; set; }
public string A_efg{ get; set; }
}
source table: no primary key
column name :abc,bcd,egf
destination table:
column name:A_abc,A_bcd,A_egf
Stored procedure for truncate and insert data from source to destination
Meghana MRank #8,429 · 222 reputationPosted Mar 25, 2023, 1:53 AM EDT
Data.cs(source class ) no primary and foreign key relationship and column values are same type but names are different between source and destination table
public class Data
{
public string Abc { get; set; }
public string cde { get; set; }
public string efg{ get; set; }
}
Data2.cs(destination class)no primary and foreign key relationship
public class data2
{
public string A_Abc { get; set; }
public string A_cde { get; set; }
public string A_efg{ get; set; }
}
source table
column name :abc,bcd,egf
destination table:
column name:A_abc,A_bcd,A_egf
error:-
'Data table requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'
after adding Hasnokey in Data2DBcontext.cs(destination.cs)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity().HasNoKey();
}
getting invalid column names error
Meghana MRank #8,429 · 222 reputationPosted Mar 24, 2023, 8:33 AM EDT
ok.Thank you