谢谢!谢谢!可能大牛人都上班去了还有个疑问就是 SQL查询到底能不能实现这种功能  ?我是一只十足的菜鸟! 唉! 比较郁闷!

解决方案 »

  1.   


              SELECT * FROM CSDN WHERE User_Name like '%Lonely_Vane%'
              --查询结果:
              --路过、、、、
      

  2.   

    create table t1(ID int,创建时间 datetime,拥有人 varchar(10))
    insert into t1 select 1,'2009-01-01','a' 
    insert into t1 select 2,'2009-01-02','b' 
    insert into t1 select 3,'2009-01-03','c' create table t2(变更时间 datetime,原拥有人 varchar(10),现拥有人 varchar(10),父ID int)
    insert into t2 select '2009-02-01','a','a1',1
    insert into t2 select '2009-03-01','a1','a',1
    insert into t2 select '2009-04-01','a','a1',1
    go
    create function f_getInfo(@ID int)
    returns @t table(ID int identity(1,1),起始时间 datetime,结束时间 datetime,原拥有人 varchar(10),现拥有人 varchar(10))
    as
    begin
        insert into @t(起始时间,结束时间,原拥有人,现拥有人)
        select 
            top 1 a.创建时间,b.变更时间,a.拥有人,b.现拥有人 
        from 
            t1 a 
        left join 
            t2 b 
        on
            a.ID=b.父ID and a.拥有人=b.原拥有人 and a.创建时间<b.变更时间
        where
            a.ID=@ID
        order by 
            b.变更时间
            while @@rowcount>0
        begin
            insert into @t(起始时间,结束时间,原拥有人,现拥有人)
            select
                a.结束时间,b.变更时间,a.现拥有人,b.现拥有人
            from
                (select top 1 * from @t order by ID desc) a
            left join
                t2 b
            on
                a.现拥有人=b.原拥有人 and b.父ID=@ID and b.变更时间>a.结束时间
            where 
                a.现拥有人 is not null
            order by 
                b.变更时间
        end    return
    end
    goselect 起始时间,结束时间,原拥有人 from dbo.f_getInfo(1)
    /*
    起始时间                                                   结束时间                                                   原拥有人       
    ------------------------------------------------------ ------------------------------------------------------ ---------- 
    2009-01-01 00:00:00.000                                2009-02-01 00:00:00.000                                a
    2009-02-01 00:00:00.000                                2009-03-01 00:00:00.000                                a1
    2009-03-01 00:00:00.000                                2009-04-01 00:00:00.000                                a
    2009-04-01 00:00:00.000                                NULL                                                   a1
    */
    godrop function f_getInfo
    drop table t1,t2
    go