Jump to content
  • Advertisement
Sign in to follow this  
jfclavette

Quick SQL question

This topic is 4264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!