我有4张桌子。 R,T,RT_imp和RT。
Table R (R_key, R_id)
Table T (T_key, T_id)
Table RT_imp (R_id, T_id, A,B,C)
使用这3个表我需要填充第4个表RT。
Table RT (R_key, T_key, A,B,C)
我需要找到id的相应键。 (R的R_key与RT_imp的R_id相关,T_key与T_id的T_id相关,与A,B,C一起插入RT)
我需要在存储过程中执行此操作。我该怎么做?
答案 0 :(得分:1)
这应该有效。我创建了临时表和别名,但您可以用表名替换它们。
Declare @R table (R_Key int, R_id int)
Declare @T table (T_Key int, T_id int)
Declare @RT_imp table (R_id int, T_id int, A char(1), B char(1), C char(1))
Declare @RT table (R_Key int, T_Key int, A char(1), B char(1), C char(1))
insert into @RT
select r.R_Key
, T_Key
, rtimp.A
, rtimp.B
, rtimp.C
from @RT_imp rtimp
inner join @R r on r.R_id = rtimp.R_id
inner join @T t on t.T_id = rtimp.T_id
答案 1 :(得分:0)
create procedure insertdata
Declare @R table (R_Key int, R_id int)
Declare @T table (T_Key int, T_id int)
Declare @RT_imp table (R_id int, T_id int, A char(1), B char(1), C char(1))
Declare @RT table (R_Key int, T_Key int, A char(1), B char(1), C char(1))
AS
BEGIN
insert into @RT
select r.R_Key, t.T_Key, rtimp.A, rtimp.B, rtimp.C
from @RT_imp rtimp, @R r, @T t
where r.R_id = rtimp.R_id
and t.T_id = rtimp.T_id
END