0
我有两个colums ID和权利的表。 Rights列将保留250个固定字符,如'010111100000000 .... 250次'。现在,我必须拆分权限列字符串,并将结果存储在临时表中,其结构ID,权限(0或1),位置(1到250)。假设我最初有5行,然后在临时表中,我将得到5 * 250 = 1250行。cte在与临时表和拆分字符串的sql服务器
我已经拆分了单个字符串并使用了游标,但现在我想避免游标。我如何才能做到这一点。
declare @temp table(Chars int not null, RowID int not null)
--Split the rights string into 250 char rows with RowID as each position
;with cte as
(
select substring(@rights, 1, 1) as Chars,
stuff(@rights, 1, 1, '') as rights,
1 as RowID
union all
select substring(rights, 1, 1) as Chars,
stuff(rights, 1, 1, '') as rights,
RowID + 1 as RowID
from cte
where len(rights) > 0
)
--Get the values in a temporary table except 0
insert into @temp select Chars, RowID from cte option (MAXRECURSION 300);
非常感谢它为我工作 – Anurag