求一条SQL语句,把三张表导入到一张递归表里,很急,很急,很急···

时间:2021-09-01 14:47:36
我的递归表
ID   Name  parentID

省表结构
ProvinceID  ProvinceName
1      北京市
2      天津市
3      河北省
4      山西省
5      内蒙古自治区

市表结构

CityID   CityName        ProvinceID
1 北京市          1
2 天津市 2
3 石家庄市 3
4 唐山市 3
5 秦皇岛市 3


区表结构

DistrictID  DistrictName    CityID
1     东城区    1
2     西城区    1
3     崇文区    1
4     宣武区    1
5     朝阳区    1
   


    请大家帮忙帮我看看怎么导入到我的递归表里,这三张表里我有数据,我就想把这三张表的数据导入到我的递归表里,但是我不知道该怎么导进去。请高手指点。谢了。我用的是SQLServer2005```

13 个解决方案

#1


递归表id是递增的吗?

#2


该回复于2010-07-29 17:52:16被版主删除

#3



DECLARE @T table (id INT ,V_NAME VARCHAR(16),parentID INT PRIMARY KEY (id))
DECLARE @Province table(ProvinceID INT ,ProvinceName VARCHAR(16) PRIMARY KEY (ProvinceID))
DECLARE @City table(CityID INT ,CityName VARCHAR(16),ProvinceID INT PRIMARY KEY (CityID))
DECLARE @District table(DistrictID INT ,DistrictName VARCHAR(16),CityID INT  PRIMARY KEY (DistrictID))

INSERT INTO @Province
SELECT 1,'北京市' union all
SELECT 2,'天津市' union all
SELECT 3,'河北省' union all
SELECT 4,'山西省' union all
SELECT 5,'内蒙古自治区' 

INSERT INTO @City
SELECT 1,'北京市',1 union all
SELECT 2,'天津市',2 union all
SELECT 3,'石家庄市',3 union all
SELECT 4,'唐山市',3 union all
SELECT 5,'秦皇岛市',3 

INSERT INTO @District
SELECT 1,'东城区',1 union all
SELECT 2,'西城区',1 union all
SELECT 3,'崇文区',1 union all
SELECT 4,'宣武区',1 union all
SELECT 5,'朝阳区',1 

INSERT INTO @T(id,V_NAME)
SELECT   ProvinceID,ProvinceName FROM @Province

INSERT INTO @T
SELECT  CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),CITYID),CityName,ProvinceID FROM @City

INSERT INTO @T
SELECT  CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),a.CityID) + CONVERT(VARCHAR(10),a.DistrictID),a.DistrictName,a.CityID 
    FROM @District  A INNER JOIN @City B  ON A.CityID = B.CityID

select * from @t

(5 行受影响)

(5 行受影响)

(5 行受影响)

(5 行受影响)

(5 行受影响)

(5 行受影响)
id          V_NAME           parentID
----------- ---------------- -----------
1           北京市              NULL
2           天津市              NULL
3           河北省              NULL
4           山西省              NULL
5           内蒙古自治区           NULL
11          北京市              1
22          天津市              2
33          石家庄市             3
34          唐山市              3
35          秦皇岛市             3
111         东城区              1
112         西城区              1
113         崇文区              1
114         宣武区              1
115         朝阳区              1

(15 行受影响)

#4


insert into 递归表
select ProvinceName,null
from 省表

insert into 递归表
select a.CityName,c.id
from 市表 a,省表 b,递归表 c
where a.ProvinceID=b.ProvinceID and b.ProvinceName=a.name

insert into 递归表
select a.DistrictName,c.id
from 区表 a,市表 b,递归表 c
where a.CityID=b.CityID and b.CityName=a.name

#5


顶起3楼

#6


DECLARE @T table (id INT IDENTITY ,NAME NVARCHAR(16),parentID INT PRIMARY KEY (id))
DECLARE @Province table(ProvinceID INT ,ProvinceName NVARCHAR(16) PRIMARY KEY (ProvinceID))
DECLARE @City table(CityID INT ,CityName NVARCHAR(16),ProvinceID INT PRIMARY KEY (CityID))
DECLARE @District table(DistrictID INT ,DistrictName NVARCHAR(16),CityID INT  PRIMARY KEY (DistrictID))

INSERT INTO @Province
SELECT 1,N'北京市' union all
SELECT 2,N'天津市' union all
SELECT 3,N'河北省' union all
SELECT 4,N'山西省' union all
SELECT 5,N'内蒙古自治区' 

INSERT INTO @City
SELECT 1,N'北京市',1 union all
SELECT 2,N'天津市',2 union all
SELECT 3,N'石家庄市',3 union all
SELECT 4,N'唐山市',3 union all
SELECT 5,N'秦皇岛市',3 

INSERT INTO @District
SELECT 1,N'东城区',1 union all
SELECT 2,N'西城区',1 union all
SELECT 3,N'崇文区',1 union all
SELECT 4,N'宣武区',1 union all
SELECT 5,N'朝阳区',1 

insert into @t select ProvinceName,null from @Province 

insert into @t select a.CityName,c.id from @City a,@Province b,@t c 
where a.ProvinceID=b.ProvinceID and b.ProvinceName=C.name 

insert into @t select a.DistrictName,c.id from @District a,@City b,@t c 
where a.CityID=b.CityID and b.CityName=C.name AND C.parentID IS NOT NULL
SELECT * FROM @t

#7


不过第三节点的插入有点问题
应该这样


INSERT INTO @T
SELECT  CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),a.CityID) + CONVERT(VARCHAR(10),a.DistrictID),a.DistrictName,CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),a.CityID)
    FROM @District  A INNER JOIN @City B  ON A.CityID = B.CityID

#8


嗯,就是的

#9


--> 测试数据: [tree]
if object_id('[tree]') is not null drop table [tree]
create table [tree] (ID int identity(1,1),Name varchar(50),parentID int)

--> 测试数据: [province]
if object_id('[province]') is not null drop table [province]
create table [province] (ProvinceID int,ProvinceName varchar(12))
insert into [province]
select 1,'北京市' union all
select 2,'天津市' union all
select 3,'河北省' union all
select 4,'山西省' union all
select 5,'内蒙古自治区'
--> 测试数据: [city]
if object_id('[city]') is not null drop table [city]
create table [city] (CityID int,CityName varchar(8),ProvinceID int)
insert into [city]
select 1,'北京市',1 union all
select 2,'天津市',2 union all
select 3,'石家庄市',3 union all
select 4,'唐山市',3 union all
select 5,'秦皇岛市',3
--> 测试数据: [area]
if object_id('[area]') is not null drop table [area]
create table [area] (DistrictID int,DistrictName varchar(6),CityID int)
insert into [area]
select 1,'东城区',1 union all
select 2,'西城区',1 union all
select 3,'崇文区',1 union all
select 4,'宣武区',1 union all
select 5,'朝阳区',1
go

insert into tree select provincename,0 from [province]
insert into tree select cityname,c.id from city a,[province] b,tree c where a.provinceid=b.provinceid and b.provincename=c.name
insert into tree select districtname,c.id from [area] a,[city] b,tree c,tree d,[province] e
where a.cityid=b.cityid and b.cityname=c.name and b.provinceid=e.provinceid and d.name=e.provincename and c.parentid=d.id


--测试:
select * from tree


/*结果:
ID          Name                                               parentID
----------- -------------------------------------------------- -----------
1           北京市                                                0
2           天津市                                                0
3           河北省                                                0
4           山西省                                                0
5           内蒙古自治区                                             0
6           北京市                                                1
7           天津市                                                2
8           石家庄市                                               3
9           唐山市                                                3
10          秦皇岛市                                               3
11          东城区                                                6
12          西城区                                                6
13          崇文区                                                6
14          宣武区                                                6
15          朝阳区                                                6

*/

#10


先把递归表的ID设成自动增长列

insert into 递归表(Name,parentID) select ProvinceName,0 from Province

insert into 递归表(Name,parentID) 
select CityName,c.ID from City a
inner join Province b on a.ProvinceID=b.ProvinceID
inner join 递归表 c on b.ProvinceName=c.Name

insert into 递归表(Name,parentID) 
select DistrictName,c.ID from District a
inner join City b on a.CityID=b.CityID
inner join 递归表 c on b.CityName=c.Name and parentID<>0

select * from 递归表
-------------------------
ID    Name    parentID
1    北京市    0
2    天津市    0
3    河北省    0
4    山西省    0
5    内蒙古自治区    0
6    北京市    1
7    天津市    2
8    石家庄市    3
9    唐山市    3
10    秦皇岛市    3
11    东城区    6
12    西城区    6
13    崇文区    6
14    宣武区    6
15    朝阳区    6

#11


谢谢大家了,我很满意···现在我的问题在你们的帮助下已经解决,真的很感谢大家···

#12


alter table 递归表 add id1 int 
alter table 递归表 add lev int

go 

insert 递归表 select name=ProvinceName,parentID=0,id1=ProvinceID ,lev=1 from 省表结构 

insert 递归表 select a.CityName,a.ProvinceID, CityID,lev=2 from 市表结构 a,递归表 b where b.id1=a.ProvinceID 

insert 递归表 select DistrictName ,b.id,DistrictID,lev=3 from 区表结构 a,递归表 b where b.id1=a.CityID and b.lev=2
 
 
select * from 递归表

ID          Name         parentID                                                                                                                                                                                                                                                         id1         lev
----------- ------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- -----------
1           北京市          0                                                                                                                                                                                                                                                                1           1
2           天津市          0                                                                                                                                                                                                                                                                2           1
3           河北省          0                                                                                                                                                                                                                                                                3           1
4           山西省          0                                                                                                                                                                                                                                                                4           1
5           内蒙古自治区       0                                                                                                                                                                                                                                                                5           1
6           北京市          1                                                                                                                                                                                                                                                                1           2
7           天津市          2                                                                                                                                                                                                                                                                2           2
8           石家庄市         3                                                                                                                                                                                                                                                                3           2
9           唐山市          3                                                                                                                                                                                                                                                                4           2
10          秦皇岛市         3                                                                                                                                                                                                                                                                5           2
11          东城区          6                                                                                                                                                                                                                                                                1           3
12          西城区          6                                                                                                                                                                                                                                                                2           3
13          崇文区          6                                                                                                                                                                                                                                                                3           3
14          宣武区          6                                                                                                                                                                                                                                                                4           3
15          朝阳区          6                                                                                                                                                                                                                                                                5           3

(15 行受影响)

#13


我还是觉得3楼这种数据格式比较好

#1


递归表id是递增的吗?

#2


该回复于2010-07-29 17:52:16被版主删除

#3



DECLARE @T table (id INT ,V_NAME VARCHAR(16),parentID INT PRIMARY KEY (id))
DECLARE @Province table(ProvinceID INT ,ProvinceName VARCHAR(16) PRIMARY KEY (ProvinceID))
DECLARE @City table(CityID INT ,CityName VARCHAR(16),ProvinceID INT PRIMARY KEY (CityID))
DECLARE @District table(DistrictID INT ,DistrictName VARCHAR(16),CityID INT  PRIMARY KEY (DistrictID))

INSERT INTO @Province
SELECT 1,'北京市' union all
SELECT 2,'天津市' union all
SELECT 3,'河北省' union all
SELECT 4,'山西省' union all
SELECT 5,'内蒙古自治区' 

INSERT INTO @City
SELECT 1,'北京市',1 union all
SELECT 2,'天津市',2 union all
SELECT 3,'石家庄市',3 union all
SELECT 4,'唐山市',3 union all
SELECT 5,'秦皇岛市',3 

INSERT INTO @District
SELECT 1,'东城区',1 union all
SELECT 2,'西城区',1 union all
SELECT 3,'崇文区',1 union all
SELECT 4,'宣武区',1 union all
SELECT 5,'朝阳区',1 

INSERT INTO @T(id,V_NAME)
SELECT   ProvinceID,ProvinceName FROM @Province

INSERT INTO @T
SELECT  CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),CITYID),CityName,ProvinceID FROM @City

INSERT INTO @T
SELECT  CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),a.CityID) + CONVERT(VARCHAR(10),a.DistrictID),a.DistrictName,a.CityID 
    FROM @District  A INNER JOIN @City B  ON A.CityID = B.CityID

select * from @t

(5 行受影响)

(5 行受影响)

(5 行受影响)

(5 行受影响)

(5 行受影响)

(5 行受影响)
id          V_NAME           parentID
----------- ---------------- -----------
1           北京市              NULL
2           天津市              NULL
3           河北省              NULL
4           山西省              NULL
5           内蒙古自治区           NULL
11          北京市              1
22          天津市              2
33          石家庄市             3
34          唐山市              3
35          秦皇岛市             3
111         东城区              1
112         西城区              1
113         崇文区              1
114         宣武区              1
115         朝阳区              1

(15 行受影响)

#4


insert into 递归表
select ProvinceName,null
from 省表

insert into 递归表
select a.CityName,c.id
from 市表 a,省表 b,递归表 c
where a.ProvinceID=b.ProvinceID and b.ProvinceName=a.name

insert into 递归表
select a.DistrictName,c.id
from 区表 a,市表 b,递归表 c
where a.CityID=b.CityID and b.CityName=a.name

#5


顶起3楼

#6


DECLARE @T table (id INT IDENTITY ,NAME NVARCHAR(16),parentID INT PRIMARY KEY (id))
DECLARE @Province table(ProvinceID INT ,ProvinceName NVARCHAR(16) PRIMARY KEY (ProvinceID))
DECLARE @City table(CityID INT ,CityName NVARCHAR(16),ProvinceID INT PRIMARY KEY (CityID))
DECLARE @District table(DistrictID INT ,DistrictName NVARCHAR(16),CityID INT  PRIMARY KEY (DistrictID))

INSERT INTO @Province
SELECT 1,N'北京市' union all
SELECT 2,N'天津市' union all
SELECT 3,N'河北省' union all
SELECT 4,N'山西省' union all
SELECT 5,N'内蒙古自治区' 

INSERT INTO @City
SELECT 1,N'北京市',1 union all
SELECT 2,N'天津市',2 union all
SELECT 3,N'石家庄市',3 union all
SELECT 4,N'唐山市',3 union all
SELECT 5,N'秦皇岛市',3 

INSERT INTO @District
SELECT 1,N'东城区',1 union all
SELECT 2,N'西城区',1 union all
SELECT 3,N'崇文区',1 union all
SELECT 4,N'宣武区',1 union all
SELECT 5,N'朝阳区',1 

insert into @t select ProvinceName,null from @Province 

insert into @t select a.CityName,c.id from @City a,@Province b,@t c 
where a.ProvinceID=b.ProvinceID and b.ProvinceName=C.name 

insert into @t select a.DistrictName,c.id from @District a,@City b,@t c 
where a.CityID=b.CityID and b.CityName=C.name AND C.parentID IS NOT NULL
SELECT * FROM @t

#7


不过第三节点的插入有点问题
应该这样


INSERT INTO @T
SELECT  CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),a.CityID) + CONVERT(VARCHAR(10),a.DistrictID),a.DistrictName,CONVERT(VARCHAR(10),ProvinceID) + CONVERT(VARCHAR(10),a.CityID)
    FROM @District  A INNER JOIN @City B  ON A.CityID = B.CityID

#8


嗯,就是的

#9


--> 测试数据: [tree]
if object_id('[tree]') is not null drop table [tree]
create table [tree] (ID int identity(1,1),Name varchar(50),parentID int)

--> 测试数据: [province]
if object_id('[province]') is not null drop table [province]
create table [province] (ProvinceID int,ProvinceName varchar(12))
insert into [province]
select 1,'北京市' union all
select 2,'天津市' union all
select 3,'河北省' union all
select 4,'山西省' union all
select 5,'内蒙古自治区'
--> 测试数据: [city]
if object_id('[city]') is not null drop table [city]
create table [city] (CityID int,CityName varchar(8),ProvinceID int)
insert into [city]
select 1,'北京市',1 union all
select 2,'天津市',2 union all
select 3,'石家庄市',3 union all
select 4,'唐山市',3 union all
select 5,'秦皇岛市',3
--> 测试数据: [area]
if object_id('[area]') is not null drop table [area]
create table [area] (DistrictID int,DistrictName varchar(6),CityID int)
insert into [area]
select 1,'东城区',1 union all
select 2,'西城区',1 union all
select 3,'崇文区',1 union all
select 4,'宣武区',1 union all
select 5,'朝阳区',1
go

insert into tree select provincename,0 from [province]
insert into tree select cityname,c.id from city a,[province] b,tree c where a.provinceid=b.provinceid and b.provincename=c.name
insert into tree select districtname,c.id from [area] a,[city] b,tree c,tree d,[province] e
where a.cityid=b.cityid and b.cityname=c.name and b.provinceid=e.provinceid and d.name=e.provincename and c.parentid=d.id


--测试:
select * from tree


/*结果:
ID          Name                                               parentID
----------- -------------------------------------------------- -----------
1           北京市                                                0
2           天津市                                                0
3           河北省                                                0
4           山西省                                                0
5           内蒙古自治区                                             0
6           北京市                                                1
7           天津市                                                2
8           石家庄市                                               3
9           唐山市                                                3
10          秦皇岛市                                               3
11          东城区                                                6
12          西城区                                                6
13          崇文区                                                6
14          宣武区                                                6
15          朝阳区                                                6

*/

#10


先把递归表的ID设成自动增长列

insert into 递归表(Name,parentID) select ProvinceName,0 from Province

insert into 递归表(Name,parentID) 
select CityName,c.ID from City a
inner join Province b on a.ProvinceID=b.ProvinceID
inner join 递归表 c on b.ProvinceName=c.Name

insert into 递归表(Name,parentID) 
select DistrictName,c.ID from District a
inner join City b on a.CityID=b.CityID
inner join 递归表 c on b.CityName=c.Name and parentID<>0

select * from 递归表
-------------------------
ID    Name    parentID
1    北京市    0
2    天津市    0
3    河北省    0
4    山西省    0
5    内蒙古自治区    0
6    北京市    1
7    天津市    2
8    石家庄市    3
9    唐山市    3
10    秦皇岛市    3
11    东城区    6
12    西城区    6
13    崇文区    6
14    宣武区    6
15    朝阳区    6

#11


谢谢大家了,我很满意···现在我的问题在你们的帮助下已经解决,真的很感谢大家···

#12


alter table 递归表 add id1 int 
alter table 递归表 add lev int

go 

insert 递归表 select name=ProvinceName,parentID=0,id1=ProvinceID ,lev=1 from 省表结构 

insert 递归表 select a.CityName,a.ProvinceID, CityID,lev=2 from 市表结构 a,递归表 b where b.id1=a.ProvinceID 

insert 递归表 select DistrictName ,b.id,DistrictID,lev=3 from 区表结构 a,递归表 b where b.id1=a.CityID and b.lev=2
 
 
select * from 递归表

ID          Name         parentID                                                                                                                                                                                                                                                         id1         lev
----------- ------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- -----------
1           北京市          0                                                                                                                                                                                                                                                                1           1
2           天津市          0                                                                                                                                                                                                                                                                2           1
3           河北省          0                                                                                                                                                                                                                                                                3           1
4           山西省          0                                                                                                                                                                                                                                                                4           1
5           内蒙古自治区       0                                                                                                                                                                                                                                                                5           1
6           北京市          1                                                                                                                                                                                                                                                                1           2
7           天津市          2                                                                                                                                                                                                                                                                2           2
8           石家庄市         3                                                                                                                                                                                                                                                                3           2
9           唐山市          3                                                                                                                                                                                                                                                                4           2
10          秦皇岛市         3                                                                                                                                                                                                                                                                5           2
11          东城区          6                                                                                                                                                                                                                                                                1           3
12          西城区          6                                                                                                                                                                                                                                                                2           3
13          崇文区          6                                                                                                                                                                                                                                                                3           3
14          宣武区          6                                                                                                                                                                                                                                                                4           3
15          朝阳区          6                                                                                                                                                                                                                                                                5           3

(15 行受影响)

#13


我还是觉得3楼这种数据格式比较好