0

我有两个临时表 #CoreUtilizationForRole1#RoleID2Details. 我想使用这两个临时表将值插入到第三个临时表中。下面是两个 Temp 表的表结构。

create table #CoreUtilizationForRole1(roleid int,SupervisorName varchar(50),ImmediateSupervisor varchar(50),UserECode varchar(50),UserName nvarchar(250),Designation varchar(250),TimeSpent float,ActivityName varchar(250))

create table #RoleID2Details(UserECode varchar(50),UserName nvarchar(250))

#CoreUtilizationForRole1表中,我生成了一份报告并存储在其中。#RoleID2Details包含如下查询中提到的数据:-

insert into #RoleID2Details
    select distinct Userecode,Username from #CoreUtilizationForRole1 where roleid=2

现在我想遍历 #RoleID2Details 的每条记录,并将数据插入到第三个临时表中,同时从#CoreUtilizationForRole1. 实际上,我需要特定描述格式的数据,因此,需要这种循环,以便我根据我的要求将数据插入到第三个临时表中,然后使用一个简单的选择语句来显示它。

第三个表结构将是 -

create table #ThirdtempTable(roleid int,SupervisorName varchar(50),ImmediateSupervisor varchar(50),UserECode varchar(50),UserName nvarchar(250),Designation varchar(250),TimeSpent float,ActivityName varchar(250))

并且插入数据的逻辑#ThirdtempTable就像这样循环表的每个记录#RoleID2Details-

insert into #ThirdtempTable
Select A.RoleId,A.SuperVisorName,A.Userecode,A.Username,A.Designation,A.TimeSpent,A.ActivityName 
from #CoreUtilizationForRole1 A 
inner join #RoleID2Details B 
on
A.SuperVisorName=B.UserName
where B.UserECode= --First UserECode from #RoleID2Details

insert into #ThirdtempTable
Select A.RoleId,A.SuperVisorName,A.Userecode,A.Username,A.Designation,A.TimeSpent,A.ActivityName 
from #CoreUtilizationForRole1 A 
inner join #RoleID2Details B 
on
A.UserName=B.UserName
where A.UserECode=--First UserECode from #RoleID2Details

这个循环将一直持续到我们从#RoleID2Details表中提取所有记录并将其插入到#ThirdtempTable表中。

4

1 回答 1

2
insert into #your3rdTempTable
select distinct t1.UserECode,t1.UserName from
#CoreUtilizationForRole1 t1 join #RoleID2Details t
on t1.UserECode=t2.UserECode and t1.UserName = t2.UserName 

你可以这样试试:

insert into #ThirdtempTable
Select A.RoleId,A.SuperVisorName,A.Userecode,A.Username,A.Designation,A.TimeSpent,A.ActivityName 
from #CoreUtilizationForRole1 A 
inner join #RoleID2Details B 
on
A.SuperVisorName=B.UserName
where B.UserECode=A.UserECode

insert into #ThirdtempTable
Select A.RoleId,A.SuperVisorName,A.Userecode,A.Username,A.Designation,A.TimeSpent,A.ActivityName 
from #CoreUtilizationForRole1 A 
inner join #RoleID2Details B 
on
A.UserName=B.UserName
where A.UserECode=A.UserECode
于 2013-04-30T06:30:06.483 回答