Unfortunately, my company still uses MS Access DBs. That’s Access 97, folks! I’ve battled many challenges over the last couple months with these databases. It hasn’t been fun, but it’s been a great learning experience.
I had a fun adventure last week dealing with one of these DBs. We have a user access DB that connects to our SQL user DB. If HR updates something in their system, it also gets updated to the Access DB system. Then we have a job that updates the SQL DB with the Access DB. The job isn’t done in SQL, though. The dev team was given a task to update that job. The dev team that was lucky enough to get this task made a simple change that should have been a cakewalk.
The update was completed easily, but no one took consideration of the dependences. We didn’t even know we had dependences on this old job/db until the emails started rolling in.
Long story short, we found the problem and were able to update it. The bad part was fixing the busted data. We found out that this DB was tied to two other MS-Access DBs. D’OH!
From there I was given the task of fixing the data in the other two DBs a.s.a.p so people could work on their documents. I thought, “No big deal, update some old Access DBs”. Then I saw the data in the DBs.
The field I had to update was tied to the UserID in the User DB. This wouldn’t have been a problem except there were many cases where there was more than one UserID in that column.
Instead of a nice USERID = 123, I had loads of USERID = 123,234.
My game plan was to take all those multiple USERIDs, split them apart, update them, then slam them back together again. How the hell was I supposed to do this in Access?
Instead of making this a novel, I’ll just get to the juicy stuff. The main goal was to update old USERIDs with new USERIDs.
The first thing I did was acquire the latest and greatest ID and toss that dude into a temp table.
SELECT USERID, MAX(USERID) AS MAXUID INTO #max
WHERE USERID <> USERID
GROUP BY USERID
Next I needed to get all the columns that had commas, split them up, and put them into a temp table. For this I used a snippet I found here that used CTE and XML
WITH Cte AS
CAST('' + REPLACE( USERID, ',' , '') + '' AS XML) AS USERID
Split.a.value('.', 'VARCHAR(100)') AS UID,
0 AS updated INTO #splitter
CROSS APPLY UID.nodes('/M') Split(a)
Next, I updated the temp table(#splitter) with the newest IDs
SET UserID = m.maxuid,
updated = 1
FROM #splitter s INNER JOIN #newUID m ON s.UID = m.uid
WHERE tableid IN (
GROUP BY tableid
HAVING COUNT(tableid) > 1)
Finally, I had to smash those guys back together as they were before. I used another temp table to put them back to together
--Using the STUFF command
STUFF(ISNULL((SELECT ', ' + x.USERID
FROM #splitter x
WHERE x.tableid = t.tableid
GROUP BY x.USERID
--AND using XML
FOR XML PATH (''), TYPE).value('.','VARCHAR(max)'), ''), 1, 2, '') AS USERID INTO #updated
FROM #splitter t
WHERE updated = 1
GROUP BY tableid
And then I updated the real table with the updated records
SET USERID = u.USERID
FROM #updated u
INNER JOIN AccessDB...CommaTable p ON u.tableid =
It was a nice adventure and hopefully it’s something I can use again.
Have you been in this situation before? What did you do? What would you do differently?