Quick SQL question

Started by
0 comments, last by Enigma 17 years, 7 months ago
I have two tables:

Table Employees:
    pkID
    fkManager  //Recursive foreign key.  Points back to Employees.pkID
    employeeName
    
TempTable:
    employeeName
    managerName

This is a fictionnal situation of the problem. Assume employeeName is unique and will never clash. Now, every columns are filled adequately, except fkManager. I want to take, in the temp table, the name of the employee, find his manager's pkID in the employee table and update the fkManager column adequately. For example:

Table Employees:
    1, NULL, Jim Jones
    2, NULL, John Doe 
    3, NULL, Jane Simmons
    4, NULL, Paul Smith
   
TempTable:
    Jim Jones, John Doe
    Jane Simmons, John Doe
    John Doe, Paul Smith

*** Insert magic query here.... ***

Table Employees:
    1, 2, Jim Jones
    2, 4, John Doe 
    3, 2, Jane Simmons
    4, NULL, Paul Smith
   
TempTable:
    Jim Jones, John Doe
    Jane Simmons, John Doe
    John Doe, Paul Smith

I'm running MSSQL Server if that can help.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Advertisement
I think it should be something like:

UPDATE Employees INNER JOIN TempTable ON Employees.employeeName = TempTable.employeeName INNER JOIN Employees AS Managers ON TempTable.managerName = Managers.employeeName SET Employees.fkManager = Managers.pkID

I don't claim to be an expert in SQL though, certainly not from a point of view of efficiency or conciseness.

Σnigma

This topic is closed to new replies.

Advertisement