mssql bom

来源:互联网 发布:淘宝客开通 编辑:程序博客网 时间:2024/06/05 16:23
sql分享 bom 
--bom结构,查找节点下所有子节点:

create table os(id int,parentid int,desn varchar(10))
insert into os select 1,0,'体育用品'
insert into os select 2,0,'户外运动'
insert into os select 3,1,'篮球'
insert into os select 4,1,'足球'
insert into os select 5,2,'帐篷'
insert into os select 6,2,'登山鞋'
insert into os select 7,0,'男士用品'
insert into os select 8,7,'刮胡刀'
insert into os select 9,3,'大号篮球'

--求个节点下所有子节点:
create function f_cid(@id int)
returns varchar(500)
as
begin
     declare @t table(id int,parentid int,desn varchar(10),lev int)
     declare @lev int
     set @lev=1
     insert into @t select *,@lev from  os where id=@id
     while(@@rowcount>0)
     begin
          set @lev=@lev+1
          insert into @t select a.*,@lev from os a,@t b
          where a.parentid=b.id and b.lev=@lev-1
     end
     declare @cids varchar(500)
     select @cids=isnull(@cids+',','')+ltrim(id) from @t order by lev
     return @cids
end
go

--调用函数
select *,ids=dbo.f_cid(id) from os


--得到每个节点路径:
create proc wsp2
@id int
as
select *,cast(' ' as varchar(10)) fullpath  into #os from os
DECLARE @i int,@j int
set @i=0
set @j=1
select @i=max(parentid) from #os
update #os set fullpath=id
while @j<=@i
begin
       update #os set fullpath=a.fullpath+','+ltrim(#os.id)
            from #os inner join #os a on #os.parentid=a.id
       where #os.parentid=@j
       set @j=@j+1
end
select * from #os
go
--调用存储过程
exec wsp2 1


----------------------------------------------------------------------------------------------------------------
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_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
  declare @level int
  set @level = 1
  insert into @t_level select @id , @level
  while @@ROWCOUNT > 0
  begin
    set @level = @level + 1
    insert into @t_level select a.id , @level
    from tb a , @t_Level b
    where a.pid = b.id and b.level = @level - 1
  end
  return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id  pid  name      
---- ---- ----------
001  NULL 广东省
002  001  广州市
003  001  深圳市
004  002  天河区
005  003  罗湖区
006  003  福田区
007  003  宝安区
008  007  西乡镇
009  007  龙华镇
010  007  松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id  pid  name      
---- ---- ----------
002  001  广州市
004  002  天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id  pid  name      
---- ---- ----------
003  001  深圳市
005  003  罗湖区
006  003  福田区
007  003  宝安区
008  007  西乡镇
009  007  龙华镇
010  007  松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid


----------------------------------------------------------------------------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER trigger [tri_updatesalary_Mem_Type]
on [dbo].[SalaryIncrease]
after  insert
as
declare @i int
set @i=@@identity

update m
set m.Mem_Type=case when s.SMark>500000 then '退休会员'--500000
                   when s.SMark>400000 then '钻石五级'--400000
                   when s.SMark>300000 then '钻石四级'--300000
                   when s.SMark>200000 then '钻石**'--200000
                   when s.SMark>100000 then '钻石二级'--100000
                   when s.SMark>50000 then '钻石一级'--50000
                   when s.SMark>40000 then '五星级'--40000
                   when s.SMark>30000 then '四星级'--30000
                   when s.SMark>20000 then '三星级'--20000
                   when s.SMark>10000 then '二星级'--10000
                   when s.SMark>5000 then '一星级'--5000
                   else '一般VIP会员'
               end
from MemberInfo m
  join (select s.SCardName,sum(s.SMark) as SMark
          from SalaryIncrease s join inserted i on s.SCardName=i.SCardName group by s.SCardName) s
    on m.Mem_Num=s.SCardName

--if exists(select * from MemberInfo m join inserted i on m.Mem_Num=i.SCardName and m.Mem_Mark>100 )
if exists (select  m.SCardName,sum(m.ShopMark)as mark from(select m.SCardName,m.ShopMark  from ShoppingMark m join inserted i on m.SCardName=i.SCardName
where year(m.SDate)=year(getdate()) and month(m.SDate)=month(getdate())) m
group by m.SCardName  having sum(m.ShopMark)>100)
/**/
/* --最新的(我写的,上面是整合魅影的)select * from ShoppingMark SM join (select m.SCardName,sum(m.ShopMark) as Mark from ShoppingMark m join inserted i on m.SCardName=i.SCardName group by m.SCardName) s
on SM.SCardName=s.ScardName where month(SDate)=month(getdate()) and year(SDate)=year(getdate()) and s.Mark>100*/
begin
update s
   set s.SIncease=case
                      --积分大于1000就是星级.所以不用判断是否是星级
                   when m.Mem_Type<>'一般VIP会员' then s.SMark*0.3
                   else case
                          when s.SMark>4000 then
                               s.SMark*0.3
                        --  when s.SMark>4000 then
                             --  200*0.2+200*0.23+200*0.25+200*0.28+(s.SMark-800)*0.3
                          when s.SMark>3000 then
                               1000*0.2+1000*0.23+1000*0.25+(s.SMark-600)*0.28
                          when s.SMark>2000 then
                               1000*0.2+100*0.23+(s.SMark-400)*0.25  
                          when s.SMark>1000 then
                               (s.SMark-200)*.023+1000*0.2
                          else  s.SMark*0.2
                       end
                  end
from SalaryIncrease as s
join inserted i
    on s.SCardName=i.SCardName
  join MemberInfo m
    on (i.SCardName=m.Mem_Num and s.SID=@i) or (i.SCardName=m.Mem_Num and s.SIncease=0)
    end
--go



--树型结构处理之双编号(广度深度排序)
if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
  drop table tb
create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
insert tb
select '0001',null,'云南省'
union all select '0002','0001','昆明市'
union all select '0003','0001','昭通市'
union all select '0009','0001','大理市'
union all select '0008',null,'四川省'
union all select '0004',null,'贵州省'
union all select '0005','0002','五华区'
union all select '0007','0002','水富县'
union all select '0006','0005','西园路192号'
union all select '0010','0006','金色梧桐3-702'
union all select '0011','0010','昆明越科时空科技有限公司'
union all select '0015','0007','两碗乡'
union all select '0013','0015','两碗村'
union all select '0012','0013','某跨国集团董事长'
union all select '0014','0008','成都市'

  
  --深度排序(模拟单编码法)
   declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
  declare @level int
  set @level=0
  insert @level_tt(ybh,ebh,level)
  select ybh,ybh,@level from tb where ebh is null
  while @@ROWCOUNT>0
  begin
          set @level=@level+1
          insert @level_tt(ybh,ebh,level)
          select a.ybh,b.ebh+a.ybh,@level
            from tb a,@level_tt b
            where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
  from tb a,@level_tt b
  where a.ybh=b.ybh
  order by b.ebh
/*
(结果)    ybh    ebh    beizhu    ybh    ebh    level
----云南省    0001    NULL    云南省    0001    0001    0
  ----昆明市    0002    0001    昆明市    0002    00010002    1
    ----五华区    0005    0002    五华区    0005    000100020005    2
      ----西园路192号    0006    0005    西园路192号    0006    0001000200050006    3
        ----金色梧桐3-702    0010    0006    金色梧桐3-702    0010    00010002000500060010    4
          ----昆明越科时空科技有限公司    0011    0010    昆明越科时空科技有限公司    0011    000100020005000600100011    5
    ----水富县    0007    0002    水富县    0007    000100020007    2
      ----两碗乡    0015    0007    两碗乡    0015    0001000200070015    3
        ----两碗村    0013    0015    两碗村    0013    00010002000700150013    4
          ----某跨国集团董事长    0012    0013    某跨国集团董事长    0012    000100020007001500130012    5
  ----昭通市    0003    0001    昭通市    0003    00010003    1
  ----大理市    0009    0001    大理市    0009    00010009    1
----贵州省    0004    NULL    贵州省    0004    0004    0
----四川省    0008    NULL    四川省    0008    0008    0
  ----成都市    0014    0008    成都市    0014    00080014    1
  */
  
  --查找子节点(包括本身节点和子节点)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
  declare @level int
  set @level=0
  insert @level_tt(ybh,ebh,level)
  select ybh,ybh,@level from tb where ybh='0005'
  while @@ROWCOUNT>0
  begin
          set @level=@level+1
          insert @level_tt(ybh,ebh,level)
          select a.ybh,b.ebh+a.ybh,@level
            from tb a,@level_tt b
            where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
  from tb a,@level_tt b
  where a.ybh=b.ybh
  order by b.ebh
/*
(无列名)    ybh    ebh    beizhu    ybh    ebh    level
----五华区    0005    0002    五华区    0005    0005    0
  ----西园路192号    0006    0005    西园路192号    0006    00050006    1
    ----金色梧桐3-702    0010    0006    金色梧桐3-702    0010    000500060010    2
      ----昆明越科时空科技有限公司    0011    0010    昆明越科时空科技有限公司    0011    0005000600100011    3
*/
转自:http://www.52mvc.com/showtopic-616.aspx