Wednesday, March 7, 2012

Insert Query Problem

Table: Dept
Dept_ID Dept_ Name
{7CFBF07C-0123} Engineering
{864CB2DF-519E} Planning
{344FDBBF-5FDD} Accounting

_
Table: Employee
Emp_ID FN LN Dept_ID
5562 John Smith
_
__
I have the above two tables. John Smith is from the Engineering Dept. I tried to insert his info _in the Employee but I couldnt because of the Dept_ID integrity. How can I write an Insert statement to automatically get the Dept_ID from the Dept Table and insert in the Employee table along for the Emp_ID, FN, LN which are known?

Check this http://www.geocities.com/oozypal/IUI/ if you can't read the tables.One way is as follows:

INSERT INTO Employee (Emp_ID, FN, LN, Dept_ID)
SELECT 5562, 'John', 'Smith', D.Dept_ID
FROM Dept AS D
WHERE D.Dept_Name = 'Engineering'

Originally posted by oozypal
Table: Dept
Dept_ID Dept_ Name
{7CFBF07C-0123} Engineering
{864CB2DF-519E} Planning
{344FDBBF-5FDD} Accounting

_
Table: Employee
Emp_ID FN LN Dept_ID
5562 John Smith
_
__
I have the above two tables. John Smith is from the Engineering Dept. I tried to insert his info _in the Employee but I couldnt because of the Dept_ID integrity. How can I write an Insert statement to automatically get the Dept_ID from the Dept Table and insert in the Employee table along for the Emp_ID, FN, LN which are known?

Check this http://www.geocities.com/oozypal/IUI/ if you can't read the tables.

No comments:

Post a Comment