|   | ![]() |
|
Now how does this work. We can
readily understand the following: insert into #fkeys select r.rkeydbid as pkdb_id, r.rkeyid as pktable_id,... r.fkeydbid as fkdb_id, r.fkeyid as fktable_id,... r.constid as fk_id, s.constid as pk_id from sysreferences r, sysconstraints s, #pivot p where r.rkeyid = s.id and (s.status & 0xf) = 1 and r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff) since all this does is just grab values from sysreferences and put them into the temp table. Now for the stuff that looks really bad. What we are taking advantage of here is a thing called characteristic functions. These are functions that will reduce to either 0 or 1. This is where the table called #pivot comes in. (Don't get the name of the table confused with the operation we are perfomring. This is a table fold and not a pivot. But I have called this a pivot table, because it helps me conceptualize what is going on and keep things straight. You can think of the sysreferences table like a sheet of paper. Take a thumb tack and place it in the top left corner. Now rotate the paper so that the long side of perpendicular to the floor. This is what sysreferences looks like. Now pivot the paper 90 degrees so that the short side is now perpendicular to the floor. This is what sysreferences is going to look like.) Keep in mind we are joining this to the #pivot table we created that has
16 rows in it numbered 1 to 16. Now to understand this, we will walk through and
evaluate each one to determine the final outcome. (Yes, the join we have specified
will produce a cross product between sysreferences and #pivot, but this is exactly what we
want.) The first row in #pivot is 1. We feed this into the first of the
three calculations: r.rkey1 * (1 - abs(sign(p.Value - 1))). p.Value - 1 = 0 because
1-1=0. sign(0) = 0. abs(0) = 0. And 1-0 = 1. We are then left with
r.key1*1. Now we feed in 2 which is the next value in #pivot. r.rkey1 * (1 -
abs(sign(p.Value - 1))) p.Value - 1 = 1 because 2 -1 = 1. sign(1) = 1.
abs(1) = 1. 1 - 1 = 0 so we are left with r.rkey1 * 0 So as you can see, the table #pivot acts as a selector to zero out all but
one of the columns. This will produce and end result of 16 rows for this table that
look like the following from this section: pkcolid =
r.rkey1 * (1 - abs(sign(p.Value - 1))) + r.rkey2 * (1 - abs(sign(p.Value - 2))) + r.rkey3
* (1 - abs(sign(p.Value - 3))) +
|
All content on this site, except where noted, represents an original work of Michael R. Hotek and is protected by applicable copyright laws. The SQL Server FAQ is the sole work of Neil Pike. No page, portion of a page, or download may be used for commercial purposes in whole or in part without the express, written permission of the applicable author.