Can I use SQL Server Table Triggers to MERGE on another table?

The TL;DR is the title, but I'll add some detail below.

I've got a conundrum.  There's a table where I've got unusually delimited data.  In one field are the names and delimiter definition, and in another is the data to be parsed.  Below is a truncated example of the fields:

RecordID PropertyNames PropertyValues
<SomeGUID> dateFormat:S:0:10:enableEmoticons:S:10:4:updateDate:S:14:87:bio:S:101:42:isEnabled:S:143:4: d MMM yyyyTrue<?xml version="1.0" encoding="utf-16"?> <dateTime>2024-06-07T04:00:00+00:00</dateTime><p>A fully-documented bio goes in here</p>True

In my database, I've got 10 or so "properties" for each record, but I've only shown five in the above example. 

To help with retrieving these individual properties, I've got a function I shamelessly stole from an online source.  It extracts out the property's value based on the Record ID and the property name.

SELECT RecordID
     , [GetPropertyValue]([RecordID], 'updateDate') AS [updateDate]
FROM [myTable]

RecordID                             updateDate
------------------------------------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
67041d13-fd39-42ee-92c7-429b7121c24e <?xml version="1.0" encoding="utf-16"?><dateTime>2024-06-07T04:00:00+00:00</dateTime>

The function works great, but it's expensive.  Let me rephrase: I built a Table-Value function and ran with a CROSS APPLY to do this for all records and it takes F-O-R-E-V-E-R. Plus it can't be indexed, so that slows down future queries.  But I like the output it gives me (below), so I started investigating moving this to an actual table in the database.

RecordID dateFormat enableEmoticons updateDate bio isEnabled
67041d13-fd39-42ee-92c7-429b7121c24e d MM yyyy True <?xml version="1.0" encoding="utf-16"?> <dateTime>2024-06-07T04:00:00+00:00</dateTime> <p>A fully-documented bio goes in here</p> True

The original table will frequently get new records, but existing records rarely change.  I thought about using a MERGE to do the work, but the original "Source" query (like the one above but with all records and all property names) takes forever to run.  Essentially, the query never gets to the INSERT/UPDATE portion.

I can run it for a handful of records at a time, so I know it works, which got me thinking: "Can I have the server do this work itself when changes are detected?"  This read very much like a trigger, but I've never worked with them before.

TL;DR

So that's the question:

  • Can I write a trigger (or three) that watches the "Source" table and when it detects an INSERT/UPDATE/DELETE, does an INSERT/UPDATE/DELETE on a different table?
  • If yes, what would that trigger definition look like?  I've already read CREATE TRIGGER (Transact-SQL) - SQL Server | Microsoft Learn, but it's over my head.  I need the K-I-S-S example.
  • Does this functionality exist in most recent versions of SQL Server?  I'm running a mix of SQL Server 2019 and 2022.

Thanks in advance for taking a moment to read through my question.

Parents
  • I used triggers EXTENSIVELY in a past gig, so I can confirm, yes you can do what you are asking.  Unfortunately I don't have any code examples handy and I'm a bit disconnected from the world to build one out right now, just using my phone from a camp site in the woods.

    This tutorial looks pretty reasonable to me: SQL Server Trigger Example (mssqltips.com)

    I'd say the big issue is you don't want it to be triggering a merge, you should try to keep the trigger much more discrete.  If one line is edited then your trigger should also just create/edit one line on the corresponding table.  Having a trigger perform a whole table action on the downstream table could be nasty for performance.

  • Appreciate it.  That's the same page I fell upon while beginning the research.  After a number of hours, I was able to get a trigger working, but the performance was horrid.  Not because of the trigger or the merge but because of the function that does the extraction.  Thankfully this is a data set that doesn't change often, so I'll just keep an eye on the execution times and see what comes.

    Appreciate the assistance.

Reply
  • Appreciate it.  That's the same page I fell upon while beginning the research.  After a number of hours, I was able to get a trigger working, but the performance was horrid.  Not because of the trigger or the merge but because of the function that does the extraction.  Thankfully this is a data set that doesn't change often, so I'll just keep an eye on the execution times and see what comes.

    Appreciate the assistance.

Children
No Data