Search code examples
sql-servertsqlt

How Does FakeTable Work with the COLUMNS_UPDATED() function?


I have a 300+ column table and a trigger. I have 40(ish) "columns of interest" and the rest I don't care about. The purpose of the trigger is to determine if any of the "columns of interest" were updated.

So: determining the COLUMNS_UPDATED() seems to be the way to go and do some bit-magic with that.

Right now: I'm Faking the 300+ table and ramming in a complete row to force the correct number of columns to ensure COLMNS_UPDATED() gets the correct bytes/bits flagged for columns changed.

My question is: is this unnecessary because the genius of FakeTable encompasses the use of COLUMNS_UPDATED() and it will return the "correct" byte indicators even if I only faked a subset of the columns?


Solution

  • tSQLt.FakeTable reconstructs the table based on the original and always includes all columns. So, after using tSQLt.ApplyTrigger, to move the trigger over to the faked table, COLUMS_UPDATED will work as designed.

    That being said, if you encounter a piece of functionality in any external tool or API that you are going to rely on in your product, and you have the feeling that there is a piece of functionality that is not documented well, it is always a good idea to write an exploratory test and check that test in with your other tests.

    In this case I would create a wide table on the fly within the test, take the table, create an update trigger on the faked table and then call tSQLt.AssertEquals within that trigger to make sure the right value is returned by COLUMNS_UPDATED.

    You achieve two things with that: A) you document the expected behavior that your code relies on. That will help future developers that have to maintain your code. B) You’ll get notified if that behavior changes.

    Now, to be clear, there’s no reason to do that with well documented functionality. But if there are documentation gaps, it’s a good practice.