--- 征求SQL语句 ---

UTF888 2004-03-29 02:36:26
比如有一表格(File):
id | parentid | filesize
1 | 0 | 100
2 | 1 | 75
3 | 1 | 25
4 | 2 | 50
5 | 2 | 25
6 | 5 | 20
7 | 6 | 5
1、求出所有id号为1的所有子对象数据(如上例显示包括:2,3,4,5,6,7);
2、更新所有id号为1的所有子对象的filesize为0
...全文
36 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-03-29
  • 打赏
  • 举报
回复
--因为你的级别是不固定的,所以一句搞不定. 也不复杂嘛,将创建函数的语句复制到查询分析器中,选择数据库,按F5执行,就建好函数了.

以后在java中直接调用就行了,也不用理会函数是怎么写的.
UTF888 2004-03-29
  • 打赏
  • 举报
回复
我是用JAVA的
UTF888 2004-03-29
  • 打赏
  • 举报
回复
一条语句搞不定啊?
我对什么数据库触发器不懂
zjcxc 元老 2004-03-29
  • 打赏
  • 举报
回复
--测试

--测试数据
create table [File](id int,parentid int,filesize int)
insert [File]
select 1,0,100
union all select 2,1,75
union all select 3,1,25
union all select 4,2,50
union all select 5,2,25
union all select 6,5,20
union all select 7,6,5
go

--创建处理用的函数
create function f_child(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select id,@l from [File] where parentid=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from [File] a join @re b on a.parentid=b.id
where b.level=@l-1
end
return
end
go

--调用实现实现处理:

--1.求出所有id号为1的所有子对象数据
select a.* from [File] a join dbo.f_child(1) b on a.id=b.id

--2、更新所有id号为1的所有子对象的filesize为0
update [File] set filesize=0
from [File] a join dbo.f_child(1) b on a.id=b.id

--显示处理结果
select * from [File]
go

--删除测试
drop table [file]
drop function f_child

/*--测试结果
id parentid filesize
----------- ----------- -----------
2 1 75
3 1 25
4 2 50
5 2 25
6 5 20
7 6 5

(所影响的行数为 6 行)


id parentid filesize
----------- ----------- -----------
1 0 100
2 1 0
3 1 0
4 2 0
5 2 0
6 5 0
7 6 0

(所影响的行数为 7 行)

--*/
zjcxc 元老 2004-03-29
  • 打赏
  • 举报
回复

--调用实现实现处理:

--1.求出所有id号为1的所有子对象数据
select a.* from [File] a join dbo.f_child(1) b on a.id=b.id

--2、更新所有id号为1的所有子对象的filesize为0
update [File] set filesize=0
from [File] a join dbo.f_child(1) b on a.id=b.id
zjcxc 元老 2004-03-29
  • 打赏
  • 举报
回复
--创建处理用的函数
create function f_child(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select id,@l from [File] where parentid=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from [File] a join @re b on a.parentid=b.id
where b.level=@l-1
end
return
end
go
leeboyan 2004-03-29
  • 打赏
  • 举报
回复
不会这么简单吧:

select * from File where id=1
update File set filesize=0 where id=1

34,838

社区成员

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

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