【IT168技术文档】
Cross Apply使表可以和表值函数结果进行join,在下面的示例中建了两个表和一个表值函数,T_b的列a_ids中会存放a表的id用,分割的字符串连接;我们通过cross apply使T_a,T_b表通过splitIDs inner join 连接。请看示例:GO
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