Question : INSERT Statement using data from other tables?

Hello,

I'm trying to write a stored procedure that will allow me to insert or update a table.  Which is simple enough, however it gets more complex for me as I have to change the data passed in the parameters to IDs on other tables.  

So for example:
TableA
UserID, UserName
1, Angus_Young_ACDC

TableB
DepartmentID, DepartmentName
3, IT Services

TableC
SecurityID, SecurityLevel
2, Medium


So the parameters I pass in are @User, @Department, @Level (Angus_young_ACDC, IT Services, Medium), and I want to have these inserted into TableD as (Columns: UserName, DepartmentType, SecurityClearance) 1,3,2.

How can I go about doing this?  As it is confusing me rather badly :)

Answer : INSERT Statement using data from other tables?

I am sorry i for got to copy the Insert statement.. Ignore the above Procedure.. Use the below procedure it will work ..

insert into tableD (UserID, DepartmentID, SecurityID)

Create proc Proc_Insert_Details
@UserName varchar(10) ,
@DepartmentName varchar(10),
@SecurityLevel varchar(10)
as
Begin
declare @User_id int
set @User_id=(select UserID FROM TABLEA WHERE UserName = @UserName)
declare @Department_id int
set @Department_id=(select DepartmentID FROM TABLEB WHERE DepartmentName = @DepartmentName)
declare @Security_id int
set @Security_id=(select SecurityID FROM TABLEC WHERE SecurityLevel = @SecurityLevel)
insert into tabled(UserID,DepartmentID,SecurityID) values(@User_id,@Department_id,@Security_id)
end
Random Solutions  
 
programming4us programming4us