从一个表中检索数据并插入另一个表

时间:2015-06-22 18:53:07

标签: sql asp.net sql-server

我正在尝试从一个表中检索数据,然后将其插入另一个表中。 这是第一个表的示例,其中包含以下数据。 tb1是由数据组成的表。 Manager和TeamLeader这两个列基本上意味着:例如:Josh由Vik管理,依此类推。员工也可以是另一个雇主的经理。例如,Josh是Nirvan和Deva的经理。

+---------+-------------+
|        tbl1           |
+---------+-------------+
| Manager | Employee    |
+---------+-------------+
| Vik     | Josh        |
+---------+-------------+
| Vik     | Cindy       |
+---------+-------------+
| Vik     | Alvin       |
+---------+-------------+
| Vik     | Kim         |
+---------+-------------+
| Josh    | Nirvan      |
+---------+-------------+
| Josh    | Deva        |
+---------+-------------+
| Cindy   | Mervyn      |
+---------+-------------+
| Nirvan  | Reeta       |
+---------+-------------+
| Nirvan  | Zaki        |
+---------+-------------+
| Nirvan  | Sunny       |
+---------+-------------+

我想要的是将所有这些记录插入到具有以下列的另一个表中:Id(设置为IDENTITY / AUTONUM),Name(员工/经理的姓名),ParentId(特定员工拥有的经理)报告)。 所以,例如, 我应该得到类似的东西:

ID Name ParentId
1  Vik    0
2  Josh   1
3  Cindy  1
4  Alvin  1
5  Kim    1
6  Nirvan 2
7  Deva   2
8  Mervyn 3
9  Reeta  6
10 Zaki   6
11 Sunny  6

我很难获得正确的sql从第一个表中检索此数据并将其插入另一个表中。

3 个答案:

答案 0 :(得分:2)

你必须分两步完成。一个用于创建所有ID字段。然后,您可以加入此表格以匹配员工,以找到经理和经理的ID:

insert into MyNewTable (Name, ParentID)
select Manager, ParentID
from tbl1
union
select Employee, ParentID 
from tbl1

update MyNewTable
set MyNewTable.ParentId = Managers.Id
from MyNewTable
join tbl1
on tbl1.Employee = MyNewTable.Name
join MyNewTable Managers
on MyNewTable.Name = Managers.Manager

答案 1 :(得分:1)

INSERT INTO tbl2 (
    Name
    ,parentId
    )
SELECT DISTINCT manager
    ,0
FROM tbl1
WHERE manager NOT IN (
        SELECT employee
        FROM tbl1
        )

INSERT INTO tbl2
SELECT DISTINCT employee
    ,0
FROM tbl1

UPDATE tbl2
SET parentid = parent.id
FROM tbl2
INNER JOIN tbl1 ON tbl2.Name = tbl1.employee
INNER JOIN tbl2 parent ON parent.Name= tbl1.manager

答案 2 :(得分:0)

create table #tbl1 (manager char(15), employee char(15))

create table #tbl2 (ID int identity(1,1), Name char(15), ParentID int)

 insert into #tbl1
 values ('Vik', 'Josh') ,
('Vik'     , 'Cindy')  ,
('Vik'     , 'Alvin') ,
('Vik'     , 'Kim')   ,
('Josh'    , 'Nirvan'),
('Josh'    , 'Deva') ,
('Cindy'   ,'Mervyn')  ,
('Nirvan'  , 'Reeta'),
('Nirvan'  , 'Zaki'),
('Nirvan'  , 'Sunny')

--- big Boss 
insert into #tbl2(Name, ParentID)
SELECT DISTINCT manager, 0
FROM #tbl1
WHERE manager NOT IN ( SELECT employee FROM #tbl1  )

--- Grab all Employees (Name)
insert into #tbl2(Name)
select distinct employee from #tbl1

--- Update ParentID 
declare @StructureLevl int = 3  -- 0 1 2 3  Do something to figure out this number first
declare @Var_int int = 0
while (@Var_int < @StructureLevl)
begin
    update #tbl2 
    set ParentID= @Var_int +1
    where Name in (select A.employee 
                    from #tbl1 A inner join #tbl2 B on A.manager = B.Name 
                    where B.ParentID = @Var_int)

    set @Var_int +=1
end

select * from #tbl2
order by ParentID