查找相关的id列,求会的人解答!!!!!!!!!!!

Dotar 2012-09-17 04:02:35
table:
id linkid
1 null
2 1
3 2
4 3
5 4
6 5

查找出有关的ID;通过where ID=6 一下查找出所有关系到的linkid 存错过程怎么写?
...全文
54 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2012-09-17
  • 打赏
  • 举报
回复

-->创建存储过程
if exists (select 1 from sysobjects where type='P' and name='Get_link')
drop proc Get_link
go
create proc Get_link(@id int)
as
begin
select a.* from tb a join cte b on a.id=b.linkid where a.id=@id
end

-->调用存储过程
exec Getlink '6'
快溜 2012-09-17
  • 打赏
  • 举报
回复
create proc Getlink(@id int)
as
begin
with cte as
(
select * from tb where id=@id
union all
select a.* from tb a join cte b on a.id=b.linkid
)

select * from cte
end
exec Getlink 6

34,590

社区成员

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

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