17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15: Triggers<br />

The COLUMNS_UPDATED() Function<br />

468<br />

This one works somewhat differently from UPDATE(), but has the same general purpose. What COLUMNS_<br />

UPDATED() gives us is the ability to check multiple columns at one time. In order to do this, the function<br />

uses a bitmask that relates individual bits in one or more bytes of varbinary data to individual columns<br />

in the table. It ends up looking something like Figure 15-4.<br />

Figure 15-4<br />

In this case, our single byte of data is telling us that the second, third, and sixth columns were<br />

updated — the rest were not.<br />

In the event that there are more than eight columns, <strong>SQL</strong> <strong>Server</strong> just adds another byte on the right-hand<br />

side and keeps counting (see Figure 15-5).<br />

0 1<br />

Figure 15-5<br />

0 1<br />

Column 1<br />

Column 2<br />

Column 3<br />

Column 4<br />

Column 5<br />

Column 6<br />

Column 7<br />

Column 8<br />

0 0 0 0 0<br />

First Byte Second Byte<br />

This time the second, ninth, and fourteenth columns were updated.<br />

0<br />

Column 1, value = 1<br />

Column 2, value = 2<br />

Column 3, value = 4<br />

Column 4, value = 8<br />

Column 5, value = 16<br />

Column 6, value = 32<br />

Column 7, value = 64<br />

Column 8, value = 128<br />

1 0 0 1 0 0<br />

1 0<br />

Column 9<br />

Column 10<br />

Column 11<br />

Column 12<br />

Column 13<br />

Column 14<br />

Column 15<br />

Column 16<br />

0 0 0 1 0 0<br />

I can hear you out there: “Gee, that’s nice — but how do I make any use of this?” Well, to answer that,<br />

we have to get into the world of boolean algebra.<br />

Making use of this information means that you need to add up the binary value of all the bytes, considering<br />

the leftmost digit to be the least significant. So, if you want your comparison to take into account 2,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!