I want to run a script on a SQL table that will search the table for the users supervisor and then replace the supervisor value with the fullname value. How can I do this? I am using MSSQL and have one table containing this data.
Before:
fullname,username,supervisor
Timothy Dalton,tdalton,rmoore
Pierce Brosnan,pbrosnan,rmoore
Sean Connery,sconnery,rmoore
Roger Moore,rmoore,dcraig
Daniel Craig,dcraig,
After script:
fullname,username,supervisor
Timothy Dalton,tdalton,Roger Moore
Pierce Brosnan,pbrosnan,Roger Moore
Sean Connery,sconnery,Roger Moore
Roger Moore,rmoore,Daniel Craig
Daniel Craig,dcraig,
Thanks
答案 0 :(得分:3)
Try something like this
Update t1
set t1.supervisor = t2.Fullname
from YourTable t1
join YourTable t2 on t1.supervisor = t2.username
This code hasn't been tested ... so make sure to backup table before using it
答案 1 :(得分:0)
尝试使用以下查询。
请注意,如果有ALTER TABLE Employee
ADD DepartmentID INT NOT NULL
Default -1;
UPDATE Employee
SET Employee.DepartmentID = @DepartmentID
WHERE DepartmentID = -1;
ALTER TABLE Employee
ADD CONSTRAINT fk_employee_to_department FOREIGN KEY (DepartmentID)
REFERENCES Department;
,则以下查询将失败。
(例如,如果multiple supervisors with same last name and first name starts with same character
为supervisor
且rmoore
中有Roger Moore
和Royal Moore
,则以下查询将使用以下任何一个名称更新主管)
full name