循环表的每个记录并将数据插入临时表中,连接SQL中的另一个临时表

问题描述:

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



I have two temporary table #CoreUtilizationForRole1 and #RoleID2Details. I want to insert values into a third temp table using these two temp table. Below are the table Structure of both the Temp table.

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包含查询中提到的数据: -





in #CoreUtilizationForRole1 table I had generated a report and stored in it. #RoleID2Details contains data as mentioned below in the query:-

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



现在我想循环遍历#RoleID2Details的每个记录,并在从#CoreUtilizationForRole1加入时将数据插入第三个临时表。实际上我需要特定描述格式的数据,因此需要这个循环,以便我按照我的要求将数据插入第三个临时表,然后使用简单的select语句我将显示它。请帮我这样做。


Now I want to loop through each records of #RoleID2Details and insert data into a third temp table while joining from #CoreUtilizationForRole1. AcTually I need data in a particular described format and due to that, this looping is required so that I insert data into third temp table as per my requirement and then using a simple select statement I will display it. Please help me to do this.

请看看:使用INSERT和SELECT添加行 [ ^ ]。



另一个解决方案是使用: CTE [ ^ ]。你没有提供足够的信息,所以我不能给你看一个例子。
Please, see this: Adding Rows by Using INSERT and SELECT[^].

Another solution is to use: CTE[^]. You did not provide enough information, so i can''t show you an example.