Hello -
I am trying to create an ANI lookup table from 2 separate tables, one a table of stores and the other a list of contacts for those stores.
I am using MS SQL Server 2005, which, unfortunately, does not support the MERGE INTO syntax...
The good stuff: The ANI lookup table has 2 significant columns, StoreID and PhoneNumber. The PhoneNumber column is the (unique) Primary key, as there must be only one StoreID returned for a given PhoneNumber.
Store_Info significant columns:
StoreID
StorePhone
AltPhone
There is one record for each StoreID, with possible duplicate phone numbers between stores. And yes, AltPhone could be the same as StorePhone...
Store_Contacts significant columns:
StoreID
Phone
There are multiple entries for StoreID, and possible duplicate phone numbers for one store or across multiple stores.
Sample store data
StoreID Parent ID StorePhone AltPhone
1 0 402-123-2300 402-123-2345
2 0 202-321-7800 202-321-7890
3 1 202-302-5600 202-302-5600
Sample contacts data:
StoreID Title Name Phone
1 Mgr Bob 402-123-2345
1 IT Pat 402-123-2346
1 Reg Mgr Dave 402-321-3213
2 Mgr Ann 202-231-7890
2 IT Mary 202-231-7893
2 A/R Ann 202-231-7890
2 Reg Mgr Dave 402-321-3213
3 Mgr Bob 402-123-2345
3 AsstMgr Pete 402-123-2356
I want to insert phone numbers in the following priority:
- Main/single store StorePhone
- Main/single store AltPhone
- Branch store StorePhone
- Branch store AltPhone
- Main/single store contact Phone
- Branch store contact phone
- If a phone number already exists in the destination table, do not add it...
So the resulting dataset should be:
StoreID Phone
1 402-123-2300 (first pass)
2 202-321-7800
1 402-123-2345 (2nd pass)
2 202-321-7890
3 202-302-5600 (3rd & 4th pass - only add once)
1 402-123-2346 (5th pass - skip dup)
1 402-321-3213
2 202-231-7893 (do not add dups)
3 402-123-2356 (final pass - skip dup)
My approach to prioritizing which phone number of the duplicates to choose is to make multiple queries based on other criteria (main store vs branch, for example), inserting the first entry found into the ANI lookup table and skipping subsequent duplicates.
How do I do this without using RBAR? I have tried the following with no luck - actually, it works OK until I get to the Store_Contacts table, where there can be multiple identical phone numbers for a given store:
INSERT INTO dbo.Store_PhoneNumbers (StoreID, PhoneNumber)
SELECT DISTINCT StoreID, dbo.GetPhoneNumber10(StorePhone)
FROM dbo.Store_Info
WHERE dbo.IsAniNumber(dbo.GetPhoneNumber10(StorePhone)) = 1
AND ParentID = 0
AND NOT EXISTS (SELECT * FROM dbo.Store_PhoneNumbers WHERE PhoneNumber = dbo.GetPhonenumber10(StorePhone));
... repeat for AltPhone, then StorePhone where ParentID <> 0 then AltPhone w/ ParentID <> 0
So far so good, then here's where it falls apart:
INSERT INTO dbo.Store_PhoneNumbers (StoreID, PhoneNumber)
SELECT DISTINCT sc.StoreID, dbo.GetPhoneNumber10(sc.Phone)
FROM Store_Contacts sc
INNER JOIN
Store_Info si ON sc.StoreID = si.StoreID
WHERE (dbo.IsAniNumber(dbo.GetPhoneNumber10(sc.Phone)) = 1)
AND (si.ParentID = 0)
AND NOT EXISTS (SELECT * FROM dbo.Store_PhoneNumbers WHERE PhoneNumber = dbo.GetPhonenumber10(sc.Phone));
... and repeat for ParentID <> 0
That's where I get the duplicate entries and the insert fails.
Thanks for any help you can give me, I'm about to give up and use a cursor, just to get it done...
Dave