都是用递归,不过这个递归是循环式的而已,并没有直接的去递归存储过程或函数.
解决方案 »
- 【新手初到】今天刚到一个公司上班,经理给了个sql语句查询的问题,要考验我 急!如果3天解决不出来就走人~~~~~~~~~~~~
- function中插入表数据返回"Error 443 invalid use of 'INSERT' within a function",难道不行吗?
- 求助:各位大哥请看我这句 SQL 语句错误在哪里?
- 问一个关于公司库表设计的问题
- 在查询分析器里,登陆本地SQLSERVER后,如何用CREATE TABLE 在远程SQL2000数据库服务器(已知IP,账号)上建立表
- sql server中一个同列相减问题
- 如何得到SQL Server实例的当前连接数?指实际连接的客户端个数。
- 请问这样的计算SQL语句怎么写,谢谢
- sql server中不用use命令,怎样打开数据库
- sql安装问题
- 想用bcp.exe,又不想装Sql Server客户端,怎么办?
- (100分)关于利用Sql2000分析服务的多维数据集做统计指标的同期比较!高手指教
create table 表(dm int,name varchar(10),pid int,xf int,yj int)
insert 表 select 1,1,0,100,500
union all select 2,2,1,200,400
union all select 3,3,2,100,200
union all select 4,4,3,100,100
go--计算的存储过程
create proc p_calc
as--初始化 yj 字段
update 表 set yj=-1--分级计算
update 表 set yj=xf
from 表 a
where not exists(
select 1 from 表 where pid=a.dm)
while @@rowcount>0
update 表 set yj=isnull(a.xf,0)+isnull(b.yj,0)
from 表 a join(
select pid,yj=sum(yj) from 表
where yj>=0 group by pid
)b on a.dm=b.pid
where a.yj=-1 and not exists(
select 1 from 表 where pid=a.dm and yj=-1)
go--调用存储过程进行计算
exec p_calc--显示计算结果
select * from 表
go--删除测试
drop table 表
drop proc p_calc/*--测试结果dm name pid xf yj
----------- ---------- ----------- ----------- -----------
1 1 1 100 500
2 2 1 200 400
3 3 2 100 200
4 4 3 100 100(所影响的行数为 4 行)
--*/