Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Sub Query in INSERT statement

How do you use a subquery to look up a value in another table to insert it into a new field?
[92 byte] By [CB77] at [2007-11-11 10:27:40]
# 1 Re: Sub Query in INSERT statement
http://www.sqlteam.com/item.asp?ItemID=277
Phil Weber at 2007-11-11 23:43:22 >
# 2 Re: Sub Query in INSERT statement
Thanks but that doesn't help me. This is what I am trying to do but get errors

INSERT INTO SportEmployee
(EmployeeLastName, EmployeeFirstName, EmployeeMI, EmployeeDOB, EmployeeManagerID, EmployeeHireDate, EmployeeHomePhone, EmployeePhotoFilename, EmployeeCommission, DepartmentID, EmployeeUserName, EmployeePassword)
VALUES ('Lee', 'Jim', 'R', 15/07/1977, (SELECT EmployeeManagerID FROM SportEmployee WHERE DepartmentID = 1), 30/6/2008, 7158889999, 'lee.jpg', 0.05, 1,'leej', '1j2k3k')
CB77 at 2007-11-11 23:44:16 >
# 3 Re: Sub Query in INSERT statement
Oh, yeah, you can't do that. :-) Why not run the SELECT query separately and save the result in a variable, then pass it to the INSERT statement along with the other values?
Phil Weber at 2007-11-11 23:45:23 >
# 4 Re: Sub Query in INSERT statement
Thanks
CB77 at 2007-11-11 23:46:17 >