Tom
Tom

Reputation: 23

SQL INSERT from two tables

I have 3 tables:

Employee(Employee_ID, First_Name, Last_Name)
Project(Project_ID, Project_Name)     
Task(Employee_ID, Project_ID, Assigned_Project_Task)

I want to insert Employee_ID from Employee to Task when I give the first and last name to the respective id (ex. Employee_ID=1 First_Name=ABC Last_Name=XYZ and I give ABC as first name and XYZ as last name, 1 will be put in Employee_ID field in the Task table), the same thing with Project_ID from Project table to Task table and lastly I need to give a name to it in the Assigned_Project_Task field in Task table.

EX. Employee_ID=1, First_Name=ABC, Last_Name=XYZ in Employee

Project_ID=10, Project_Name=SomeProject in Project

If I give the following answers(in windows form in C#) first_name=ABC, last_name=XYZ and project_name=SomeProject and name the task=NewTask. (I give the task name from a txtbox in windows form)

It will put the in the Task table the following:

Employee_ID=1, Project_ID=10, Assigned_Project_Name=NewTask

How do I do this insert?

Upvotes: 2

Views: 98

Answers (1)

Tim3880
Tim3880

Reputation: 2583

You insert statement should look like this

INSERT INTO Task (Employee_ID, Project_Id, Assigned_Project_Name)
SELECT e.Employee_Id , p.Project_Id, 'NewTask'
FROM Employees e INNER JOIN Projects p 
WHERE e.last_name='XYZ' AND e.first_name='ABC' 
AND p.Project_Name='SomeProject';

You need find out how to build this query in C# yourself.

Upvotes: 5

Related Questions