输入banner图图片脚本导航/分类

SQL语句复制父子级表数据


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