重写SQL Server Maxrecursion?
我有一张结构像这样的桌子重写SQL Server Maxrecursion?,sql,sql-server,tsql,recursion,Sql,Sql Server,Tsql,Recursion,我有一张结构像这样的桌子 name properties x thing1, thing2, thing3 y otherthing1, otherthing2, otherthing3 我想把它映射到一对多的关系,比如 name properties x thing1 x thing2 x thing3 我使用的以下解决方案可以工作,但会遇到SQL Server中的Maxrecursion选项 ;with tmp(brandName,
name properties
x thing1, thing2, thing3
y otherthing1, otherthing2, otherthing3
我想把它映射到一对多的关系,比如
name properties
x thing1
x thing2
x thing3
我使用的以下解决方案可以工作,但会遇到SQL Server中的Maxrecursion选项
;with tmp(brandName, drugList, genericName) as (
select brandName, LEFT(genericName, CHARINDEX(',',genericName+',')-1),
STUFF(genericName, 1, CHARINDEX(',',genericName+','), '')
from eeeee
where LEN(genericName) - LEN(replace(genericName,',','')) < 100
union all
select brandName, LEFT(genericName, CHARINDEX(',',genericName+',')-1),
STUFF(genericName, 1, CHARINDEX(',',genericName+','), '')
from tmp
where genericName > ''
)
select brandName, drugList
from tmp
order by brandName
where子句是让我们运行此查询的原因,因为多值列中的某些行在列表中有超过100项。是否有任何方法可以绕过SQL Server对递归的最大100个限制?还是最好直接将超过100个值的列拆分为两个,然后执行递归?我可以建议不使用递归的更有效的解决方案-
DECLARE @temp TABLE
(
name NVARCHAR(50)
, properties NVARCHAR(1000)
)
INSERT INTO @temp (name, properties)
VALUES
('x', 'thing1, thing2, thing3'),
('y', 'otherthing1, otherthing2, otherthing3')
SELECT
data.name
, property = LTRIM(data.property)
FROM (
SELECT
name = p.value('(./n)[1]', 'NVARCHAR(50)')
, property = p.value('(./s)[1]', 'NVARCHAR(1000)')
FROM (
SELECT field = CAST('<r><s>' + REPLACE(t.properties + ',', ',', '</s><n>' + t.name + '</n></r><r><s>') + '</s></r>' AS XML)
FROM @temp t
) d
CROSS APPLY field.nodes('/r') t(p)
) data
WHERE data.name IS NOT NULL
当然可以。表中的每个字符串都有一个固定的分隔符char。我们将分隔符字符替换为XML结构的一部分。我们有这样的台词:
thing1, thing2, thing3 -> <r><s>thing1</s><s>thing2</s><s>thing3</s></r>
将字符串转换为XML日期类型并分析形成的树:
<r>
<s>thing1</s>
<n>x</n>
</r>
<r>
<s> thing2</s>
<n>x</n>
</r>
<r>
<s> thing3</s>
<n>x</n>
</r>
在末尾使用类似选项MAXRECURSION 0I尝试了选项MAXRECURSION 100,显然我不知道如何实现它+1MAXRECURSION 100是默认值。MAXRECURSION 0将最大递归重写为无限制。Per MSDN:指定此查询允许的最大递归数。数字是介于0和32767之间的非负整数。指定0时,不应用任何限制。如果未指定此选项,则服务器的默认限制为100。这非常有趣。想解释一下它是如何工作的吗?我以前从未见过类似的情况,现在回到这个答案,你解释了它是如何工作的。谢谢