t1
A B C D
1a 1b 1c 1d
t2
E F G H
1e 1f 1g 1h
Result
E F G H R
1e 1f 1g 1j 1a,1b,1c,1d
我想得到result 但是发现FOR XML PATH 效率堪忧。大神们有没有更好的方法啊
4 个解决方案
#1
这个直接内连接(或外连接)就可以了,不用for xml path 这个语法的。
#2
两表没关联列,笛卡尔集?
#3
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#t1') is null
drop table #t1
Go
Create table #t1([A] nvarchar(22),[B] nvarchar(22),[C] nvarchar(22),[D] nvarchar(22))
Insert #t1
select N'1a',N'1b',N'1c',N'1d'
Go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#t2') is null
drop table #t2
Go
Create table #t2([E] nvarchar(22),[F] nvarchar(22),[G] nvarchar(22),[H] nvarchar(22))
Insert #t2
select N'1e',N'1f',N'1g',N'1h'
GO
SELECT
b.E,b.F,b.G,b.H,a.R
FROM
(Select A+','+B+','+C+','+D AS R,ROW_NUMBER()OVER(ORDER BY RAND()) AS RN from #t1) AS a
INNER JOIN (Select *,ROW_NUMBER()OVER(ORDER BY RAND()) AS RN from #t2) AS b ON a.RN=b.RN
/*
E F G H R
1e 1f 1g 1h 1a,1b,1c,1d
*/
#4
你的t1表是多列,也不是多行啊,干嘛要用FOR XML PATH呢
#1
这个直接内连接(或外连接)就可以了,不用for xml path 这个语法的。
#2
两表没关联列,笛卡尔集?
#3
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#t1') is null
drop table #t1
Go
Create table #t1([A] nvarchar(22),[B] nvarchar(22),[C] nvarchar(22),[D] nvarchar(22))
Insert #t1
select N'1a',N'1b',N'1c',N'1d'
Go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#t2') is null
drop table #t2
Go
Create table #t2([E] nvarchar(22),[F] nvarchar(22),[G] nvarchar(22),[H] nvarchar(22))
Insert #t2
select N'1e',N'1f',N'1g',N'1h'
GO
SELECT
b.E,b.F,b.G,b.H,a.R
FROM
(Select A+','+B+','+C+','+D AS R,ROW_NUMBER()OVER(ORDER BY RAND()) AS RN from #t1) AS a
INNER JOIN (Select *,ROW_NUMBER()OVER(ORDER BY RAND()) AS RN from #t2) AS b ON a.RN=b.RN
/*
E F G H R
1e 1f 1g 1h 1a,1b,1c,1d
*/
#4
你的t1表是多列,也不是多行啊,干嘛要用FOR XML PATH呢