• Sql Server2005对tsql的增强之Cross Apply


    Cross Apply使表可以和表值函数结果进行join,在下面的示例中建了两个表和一个表值函数,T_b的列a_ids中会存放a表的id,分割的字符串连接;我们通过cross apply使T_aT_b表通过splitIDs inner join 连接。请看示例:GO

    if object_id('T_a','U') is not null
    drop table T_a
    GO

    CREATE TABLE T_a(    id int unique not null,
        name varchar(50),
    )
    GO
    if object_id('T_b',N'U') is not null
    drop table T_b
    GO
    create table T_b
    (
        id int unique not null,
        name varchar(10),
        a_ids varchar(100) null --要在这一列中存放t_a表的ID序列,这样做连第一范式都没有满足,但是有时候考虑性能或设计我们可能会像这么用
    )
    GO
    --初始化数据

    INSERT INTO T_a VALUES(1,'A-1')
    INSERT INTO T_a VALUES(2,'A-2')
    INSERT INTO T_a VALUES(3,'A-3')
    INSERT INTO T_a VALUES(4,'A-4')
    INSERT INTO T_a VALUES(5,'A-5')

    INSERT INTO T_b VALUES(1,'B-1','1,2,4')
    GO
    --创建一个表值函数,用来拆分用逗号分割的数字串,返回只有一列数字的表
    if object_id('splitIDs','TF') is not null
    drop function splitIDs;
    GO
    create function splitIDs(
        @Ids nvarchar(1000)
    )
    returns @t_id TABLE (id bigint)
    as
    begin
        declare @i int,@j int,@l int,@v bigint;
        set @i = 0;
        set @j = 0;
        set @l = len(@Ids);
        while(@j < @l)
        begin
           set @j = charindex(',',@Ids,@i+1);
           if(@j = 0) set @j = @l+1;
           set @v = cast(SUBSTRING(@Ids,@i+1,@j-@i-1) as bigint);
           INSERT INTO @t_id VALUES(@v)
           set @i = @j;
        end
        return;
    end
    GO
    --测试splitIDs的执行效果

    select * from splitIDs('1,2,4,3')
    select * from splitIDs('100')
    select * from splitIDs(NULL)
    GO
    --使用cross apply获得t_b表中指定行对应的所有t_a表中的记录
    select 
        aid = t_a.id
        ,aname = t_a.name
        ,bid = t_b.id
    from t_b

    cross apply splitIDs(a_ids) tbl_Ids
    INNER JOIN t_a ON tbl_Ids .id = t_a.id
    where t_b.id = 1

    你明白cross apply的用法了吗?有问题欢迎讨论。
    ----------------------- 

    相关随笔
    · Sql Server2005对t-sql的增强之通用表表达式CTE
    · Sql Server2005对t-sql的增强之top
    · 用Clr实现的sql表值函数splitIDs
    · Sql Server2005对t-sql的增强之排名函数

  • 相关阅读:
    loadrunder之脚本篇——action分类
    性能测试工具下载
    Loadrunder常见问题汇总(持续更新)
    python编程总结
    使用 rsync 同步
    mysql查询今天、昨天、7天、近30天、本月、上一月 数据
    vim配置
    shell 分割字符串存至数组
    变换莫测
    2014 10
  • 原文地址:https://www.cnblogs.com/yukaizhao/p/cross_apply.html
Copyright © 2020-2023  润新知