好象是表的自己与自己连接,不知道怎么写SQL好

jjfjszjg 2003-10-16 12:41:22
这里有一个表catalog
字段:id parent_id TAGNO
例如:100 1 100-01-001
101 1 100-01-002
500 100 (这里要被赋值)
501 100
502 100
503 100
....
800 101
801 101
802 101
如何写一个SQL使得子结点的TAGNO与父结点的TAGNO一致
例如 500,100,100-01-001
update catalog a
set a.tagno=(select tagno from catalog b where a.parent_id=b.id)
这样写不对,但就是这个意思。
好象是表的自己与自己连接,不知道怎么写好。
...全文
115 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
伍子V5 2003-10-16
  • 打赏
  • 举报
回复
try:
update catalog
set tagno=b.tagno from catalog b where b.id=catalog.parent_id
伍子V5 2003-10-16
  • 打赏
  • 举报
回复
try:
update catalog a
set a.tagno=b.tagno from catalog b where b.id=a.parent_id

michealin 2003-10-16
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2087/2087118.xml?temp=.1218378

对你有用
pengdali 2003-10-16
  • 打赏
  • 举报
回复
一个循环即可。


create table #catalog(id int,parent_id int,TAGNO varchar(20))
insert #catalog values(100, 1 ,'100-01-001')
insert #catalog values(101 , 1 , '100-01-002')
insert #catalog values(500 , 100 ,null)
insert #catalog values(501 ,100 ,null)
insert #catalog values(502 ,100,null)
insert #catalog values(503 , 100,null)
insert #catalog values(800 , 101 ,null)
insert #catalog values(801 , 101,null)
insert #catalog values(802 , 101,null)


--开始

while exists(select 1 from #catalog where parent_id in (select id from #catalog where tagno is not null) and tagno is null)
update #catalog set tagno=(select tem.tagno from #catalog tem where tem.id=#catalog.parent_id and tem.tagno is not null) where parent_id in (select id from #catalog where tagno is not null) and tagno is null

--结束

go
drop table #catalog
---涛声依旧--- 2003-10-16
  • 打赏
  • 举报
回复
update catalog
set tagno=b.tagno
from catalog,catalog b where b.id=catalog.parent_id
jjfjszjg 2003-10-16
  • 打赏
  • 举报
回复
收益非浅
yujohny 2003-10-16
  • 打赏
  • 举报
回复
update a
set a.tagno=b.tagno
from catalog a inner join catalog b on a.parent_id=b.id
zjcxc 元老 2003-10-16
  • 打赏
  • 举报
回复
我的方法,即使结点层数有N层,也可以保证其下面的所有子结点可以被更新到.

这种类型数据的其他处理方法可以参考:
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=1.794696E-04
zjcxc 元老 2003-10-16
  • 打赏
  • 举报
回复
--创建一个函数,得到指定结点的编码累计:

--自定义函数--获取编码累计
create function f_getmergid(@id int)
returns varchar(8000)
as
begin
declare @re varchar(8000),@pid int

--为了数字排序正常,需要统一编码宽度
declare @idlen int,@idheader varchar(20)
select @idlen=max(len(id))
,@idheader=space(@idlen)
from catalog

--得到编码累计
set @re=right(@idheader+cast(@id as varchar),@idlen)
select @pid=parent_id from catalog where id=@id
while @@rowcount>0
select @re=right(@idheader+cast(@pid as varchar),@idlen)+','+@re
,@pid=parent_id from catalog where id=@pid
return(@re)
end
go

--调用这个函数进行更新处理

update catalog set TAGNO=b.TAGNO
from catalog a,(select mid=dbo.f_getmergid(id)+'%',TAGNO from catalog where TAGNO<>'') b
where a.TAGNO='' and dbo.f_getmergid(id) like b.mid

maoyesky 2003-10-16
  • 打赏
  • 举报
回复

update catalog set tagno=(select tagno from catalog b where b.id=catalog.parent_id) where parent_id in (select parent_id from catalog)
LoveSQL 2003-10-16
  • 打赏
  • 举报
回复
update a
set a.tagno=b.tagno
from catalog a,catalog b
where a.parent_id=b.id
playyuer 2003-10-16
  • 打赏
  • 举报
回复
update catalog
set tagno=b.tagno
from catalog,catalog b where b.id=catalog.parent_id
LoveSQL 2003-10-16
  • 打赏
  • 举报
回复
update a
set a.tagno=b.tango
from catalog a,catalog b
where a.parent_id=b.id
jjfjszjg 2003-10-16
  • 打赏
  • 举报
回复
服务器: 消息 107,级别 16,状态 3,行 1
列前缀 'catalog' 与查询中所用的表名或别名不匹配。

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧