2
That's fixed Thanks!
I have one more doubt instead of updating all the records again and again can we update only the records which data is changed against the column level
2
@Krishna Reddy , it means you are trying to insert UserId in the table that either do not exist in primary key table or you are simply not providing UserId column to the table which is foreign key and not null in nature.
2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tRTUser_Infos_tRTUsers_user_ou_info_user_id". The conflict occurred in database "myddname", table "dbo.tRTUsers", column 'user_id'.
2
Hi Krishna,
please refer this link:
https://codingsight.com/different-ways-to-compare-sql-server-tables-schema-and-data/
2
Here you go.
- /* add new records */
- insert into table1 (PersNum, name , Surname)
- select t2.PersNum, t2.name , t2.Surname
- from table2 t2
- where not exists(
- select 1 from table1 t1
- where t1.PersNum = t2. PersNum
- );
-
-
- /* update existing records */
- UPDATE
- table1
- SET
- name = table2.name, Surname = table2.Surname
- FROM
- table2
- where table1.PersNum = table2.PersNum;
-
-
- select * from table1
2
you can try Merge
- MERGE dbo.SomeTable AS target
-
- USING dbo.AnotherTable AS source
-
- ON (target.SomeColumn = source.AnotherColumn)
-
-
-
- WHEN MATCHED THEN
- UPDATE SET Name = source.Name,
- OtherCol = source.SomeCol
-
-
-
- WHEN NOT MATCHED THEN
- INSERT (Col1, Col2, ...., ColN)
- VALUES (source.Val1, source.Val2, ...., source.ValN);