当前位置:Gxlcms > 数据库问题 > SQL语句复制父子级表数据

SQL语句复制父子级表数据

时间:2021-07-01 10:21:17 帮助过:37人阅读


103 语文 100
104 第三层 102
105 英语 102

……………………

 

--第一步:复制全部数据
Insert Into Book(text,pid) Select a.text,a.pid From Book a where 条件

--第二步:创建变量表
declare @t table(idx int,id int,pid int,id2 int,pid2 int)
Insert Into @t
select ROW_NUMBER() OVER (ORDER BY a.pid) AS idx,a.id,a.pid,b.id id2,b.pid pid2
from Book a inner join Book b on b.text=a.text and b.pid=a.pid where 条件

--第三步:修改新数据的pid
update Book set pid=c.id2
from @t a
inner join @t b on b.pid=a.id
inner join @t c on c.idx=a.idx

 

SQL语句复制父子级表数据

标签:set   自增   color   英语   sql   bsp   order   join   table   

人气教程排行