打印本文 打印本文 关闭窗口 关闭窗口
SQL Server 中 自定义函数 和 游标 应用的经典案例
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4604  更新时间:2007/11/14 12:58:09  文章录入:mintao  责任编辑:mintao
    declare myCur cursor for ( select 管理员 from 2 where 2.NAME = @name )

    open myCur

    fetch next from myCur into @tempStr

    set @flag = 0

    while @@fetch_status = 0

    begin

        if @flag = 0

    begin

        set @nameStr = @tempStr

    end

    else

    begin

        set @nameStr = @nameStr + '''','''' + @tempStr

    end

    set @flag = @flag + 1

    fetch next from myCur into @tempStr

    end

    close myCur

    deallocate myCur

    return @nameStr

end

 

select 2.NAME as 姓名, count(ID) as 上机次数, dbo.GetNameStr(2.NAME) as 管理员

from 2

where 2.NAME in ( select 1.NAME from 1 )

group by 2.NAME

 

 

测试结果:

上一页  [1] [2] [3] [4] [5]  下一页

打印本文 打印本文 关闭窗口 关闭窗口