2

I need to update several rows from a table in a single operation.

What I'd normally do, is to delete (soft delete) all previous records with the same foreign key and insert everything again. But this is not optimal at all and now that my job actually involves having to create stored procedures I wanted to know it there was a 'standard' way to perform this kind of operations.

Take these images as an example:

  1. This is the original data associated with the foreign key (ParentId) 1:

    enter image description here

  2. Then using the corresponding interface I make the following changes:

    enter image description here

    a) In the second row, the column Name (in yellow) was changed
    b) The third row (in red) was removed
    c) The fourth and fifth rows (in green) are new. Because these rows reside only on the client side I don't have a primary key (Id) for them

  3. This is what I would like to end up with:

    enter image description here

So far I've created a new type :

IF EXISTS(SELECT * FROM sys.types WHERE name='T_DetailDomain') BEGIN DROP TYPE dbo.T_DetailDomain; END; GO CREATE TYPE dbo.T_DetailDomain AS TABLE ( Id INT NOT NULL, Name VARCHAR (50) NOT NULL, Abbrev VARCHAR (300) NOT NULL, ParentId INT NOT NULL ); GO 

And this is the signature of the procedure:

CREATE PROC dbo.up_DetailDomain_Update @pParentId INT, @pTableDetail dbo.T_DetailDomain READONLY AS 

As you can see I pass all the rows as a table-valued parameter (@pTableDetail).

Is there any 'standard' way I can achieve what I described above?

Or do I have to perform three operations:

  1. Insert all the rows without an Id
  2. update all the rows with an existing Id
  3. remove all the rows with an Id not present in the table-valued parameter?

Thanks.

4
  • 1
    You are looking to MERGE data. you can use merge (msdn.microsoft.com/en-us/library/bb510625.aspx) there are instances not to do so performance wise and things but then you just first an update statement with inner join then an insert statement and then a delete statement constrained to non joined records.... Commented Sep 15, 2016 at 22:49
  • stackoverflow.com/questions/14806768/… Commented Sep 15, 2016 at 22:51
  • @Matt But is it common to use this Merge statement to perform an update? Would you use it yourself? I'm a front-end developer taking its first steps into the SQL world, so please bear with me. Commented Sep 15, 2016 at 22:57
  • yeah it is pretty common but as an ETL developer for larger datasets I still prefer the 3 statements. there are a few issues with MERGE mssqltips.com/sqlservertip/3074/… but most of the issues with it are edge cases if you have a pretty simple data base structure meaning not lots of triggers etc. I will say performance is better with the 3 separate statements but if you are not working with large datasets it likely won't matter Commented Sep 15, 2016 at 23:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.