Hey everyone. I hope this is something easy that I'm overlooking. I've been given a task to build a little stored procedure that will make sure some values always stay the same. Here's what I've got:tblEmployeeempID (p/k, int)empFName (v/c, 25)empLName (v/c 25)empNumber (v/c 10) ...tblOfficeoffID (p/k, int)offName (v/c 35)offAddress (v/c 100) ...tblOfficeRoutesorID (p/k int)empID (f/k, int)offID (f/k, int) ...tblLastCheckslcEmpNum (v/c 10)currOffID (int)lastOffID (int)
I didn't build this part but I have to work with it the way it is. So, here's what happens. You got employees who have specific offices they visit (some visit 3 or 4 and some only 1). So in tblOfficeRoutes, you'll have their foreign key to the tblEmployee table and the foreign key to the Offices that they visit. There is also a tblLastChecks table which (and I don't know why they did it this way) lists the Employee Number (not the ID but their number) and the last office they were at and the one that they are currently at.The problem seems to be that even though employee 123 is only supposed to visit 1 office, sometimes they will have two different numbers in tblLastChecks. I've never seen it but I've been told it has happened and now they want a bandaid. I'm thinking the best approach (if this is possible) would be to have the stored procedure get a list of all empID's from tblOfficeRoutes who only have 1 office in their list, then it will update the tblLastChecks table and set BOTH currOffID and lastOffID to whatever the single offID field is (from tblOfficeRoutes) for that empID.This is not my database and I can't "change" anything because it's hit by a lot of customers/employees/etc. So I've been tasked to create the bandaid stored procedure.I've seen some code like this:UPDATE PSET People.LastName = Schools.[School Name] FROM Schools JOIN People AS P ON P.SomeColumn = Schools.SomeColumn
but I'm not sure how that would work when there are 2 fields to be updated? Would that mean you've got to have 2 SETS (each with a subquery)? I've seen some examples of using "HAVING COUNT" so I think that would be part of it. But I can't figure out how to do this.I tried: select empID from tblOfficeRoutes GROUP BY empID HAVING COUNT(empID) = 1
but that returns employees who have more than one listing in tblOfficeRoutes.As I'm sure you can tell from this crazy post I'm lost. I've done some work with SubQueries but Updating from a Select? Way beyond what I can figure out.Any suggestions (other than setting a torch to the database and starting from scratch)?Any and all help/suggestions/ideas is greatly appreciated.DTFan <- banging head against wall next to desk
DTFanEver-hopeful programmer-in-training 