读书人

存储过程 循环往上查找推荐人!解决办

发布时间: 2012-04-05 12:42:40 作者: rapoo

存储过程 循环往上查找推荐人!!!!!!!!!!!!
有一张用户表,里面的用户有推荐关系!

我现在新增了几个新用户!

我想往推荐人身上加积分!

假如 A→B→C→D→E→F→G→H
→表示推荐

我现在获取到了H H用户有一个字段是他的推荐人ID

我如何往G上加完推荐积分,
然后在查G的推荐人F,往F头上加积分之后
又查到F的推荐人E。这样一直循环加到A


用游标可以实现嘛 !??

[解决办法]

SQL code
if object_id('[tb]') is not null drop table [tb]create table [tb] (id int,name varchar(1),pid int)insert into [tb]select 1,'A',0 union allselect 2,'B',1 union allselect 3,'D',1 union allselect 4,'C',2 union allselect 5,'D',2 union allselect 6,'A',4 union allselect 7,'E',5 union allselect 8,'F',5GO;with cteas(    select   *,[path]=cast([name]+'->' as varchar(100)) ,[level] = 1 from tb where pid = 0    union all    select a.*,  cast(c.[path]+a.[name]+'->' as varchar(100)),[level]+1 from cte c ,tb a where a.pid = c.id)select * from ctewhere len([path]) > 6 and right([path],3) = left([path],3)/*id          name pid         path           level----------- ---- ----------- -------------- -----6           A    4           A->B->C->A->     4(1 行受影响)*/-------------------------------------- Author : happyflystone  -- Date   : 2010-04-06 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) --          Apr 14 2006 01:12:25 --          Copyright (c) 1988-2005 Microsoft Corporation--          Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)--      -------------------------------------- Test Data: taIF OBJECT_ID('[tb]') IS NOT NULL     DROP TABLE [tb]GoCREATE TABLE tb([cid] NVARCHAR(1),[pid] NVARCHAR(1))GoINSERT INTO tb    SELECT 'A','B' UNION ALL    SELECT 'A','D' UNION ALL    SELECT 'B','C' UNION ALL    SELECT 'B','D' UNION ALL    SELECT 'C','A' UNION ALL    SELECT 'D','E' UNION ALL    SELECT 'D','F' GO--Start;with cteas(    select   *,[path]=cast([cid]+'->' as varchar(100)) ,[level] = 1     from (select distinct cid,cast('' as nvarchar(1))  as pid from tb union  select distinct pid ,'' from tb) b     union all    select a.*,cast(a.[cid]+'->'+c.[path] as varchar(100)),[level]+1     from cte c ,tb a     where a.pid = c.cid and charindex(a.[cid]+'->',c.[path])=0)select [path]+cid+'->'from ctewhere exists(select 2 from tb where cid+'->' = right([path],3) and pid+'->' = left([path],3))-- = left([path],3)--Result:/*--------------A->B->C->A->C->A->B->C->B->C->A->B->(3 行受影响)*/--End 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2010/04/06/5456223.aspxBOM循环问题,参考上诉资料
[解决办法]
参考下面的内容,自己修改一下即可.

SQL code
/*标题:SQL SERVER 2000中查询指定节点及其所有父节点的函数(表格形式显示)作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12地点:广东深圳*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))insert into tb values('001' , null  , '广东省')insert into tb values('002' , '001' , '广州市')insert into tb values('003' , '001' , '深圳市')insert into tb values('004' , '002' , '天河区')insert into tb values('005' , '003' , '罗湖区')insert into tb values('006' , '003' , '福田区')insert into tb values('007' , '003' , '宝安区')insert into tb values('008' , '007' , '西乡镇')insert into tb values('009' , '007' , '龙华镇')insert into tb values('010' , '007' , '松岗镇')go--查询指定节点及其所有父节点的函数create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))asbegin  insert into @t_level select @id  select @id = pid from tb where id = @id and pid is not null  while @@ROWCOUNT > 0  begin    insert into @t_level select @id     select @id = pid from tb where id = @id and pid is not null  end  returnendgo--调用函数查询002(广州市)及其所有父节点select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id/*id   pid  name       ---- ---- ---------- 001  NULL 广东省002  001  广州市(所影响的行数为 2 行)*/--调用函数查询003(深圳市)及其所有父节点select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id/*id   pid  name       ---- ---- ---------- 001  NULL 广东省003  001  深圳市(所影响的行数为 2 行)*/--调用函数查询008(西乡镇)及其所有父节点select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id/*id   pid  name       ---- ---- ---------- 001  NULL 广东省003  001  深圳市007  003  宝安区008  007  西乡镇(所影响的行数为 4 行)*/drop table tbdrop function f_pid 


[解决办法]
那你看看如下的示例吧:

SQL code
/*标题:SER SERVER 2005中统计各节点及其子节点的数量及合计(逐级汇总)作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2011-05-06地点:重庆航天职业学院id   pid  name       cnt---- ---- ---------- -----------001  NULL 广东省        0002  001  广州市        0003  001  深圳市        0004  002  天河区        0005  003  罗湖区        0006  003  福田区        0007  003  宝安区        0008  007  西乡镇        1009  007  龙华镇        2010  007  松岗镇        3011  006  岗下镇        4012  005  上沙镇        5013  004  天河镇        6-->统计结果如下:id   name       unit        sum_cnt---- ---------- ----------- -----------001  广东省        13          21002  广州市        3           6003  深圳市        9           15004  天河区        2           6005  罗湖区        2           5006  福田区        2           4007  宝安区        4           6008  西乡镇        1           1009  龙华镇        1           2010  松岗镇        1           3011  岗下镇        1           4012  上沙镇        1           5013  天河镇        1           6*/create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10) , cnt int)insert into tb values('001' , null  , N'广东省' , 0)insert into tb values('002' , '001' , N'广州市' , 0)insert into tb values('003' , '001' , N'深圳市' , 0)insert into tb values('004' , '002' , N'天河区' , 0)insert into tb values('005' , '003' , N'罗湖区' , 0)insert into tb values('006' , '003' , N'福田区' , 0)insert into tb values('007' , '003' , N'宝安区' , 0)insert into tb values('008' , '007' , N'西乡镇' , 1)insert into tb values('009' , '007' , N'龙华镇' , 2)insert into tb values('010' , '007' , N'松岗镇' , 3)insert into tb values('011' , '006' , N'岗下镇' , 4)insert into tb values('012' , '005' , N'上沙镇' , 5)insert into tb values('013' , '004' , N'天河镇' , 6)go;WITH T AS(  SELECT ID , PID , NAME , CNT FROM TB   UNION ALL  SELECT B.ID , A.PID , A.NAME , B.CNT FROM TB AS A JOIN T AS B ON A.ID = B.PID )SELECT p.id , p.name , count(1) unit, sum(t.cnt) sum_cnt FROM tb p , t where isnull(p.pid,'000') = isnull(t.pid,'000') and p.name = t.name group by p.id , p.name order by p.id drop table tb
[解决办法]
很想知道 书画 是做什么呢。每次都贴 一大堆,地方都不同。哥们是不是天天多地飞呀。也发现最近开始出来抢分了。

读书人网 >SQL Server

热点推荐